Skip to content

Commit e2a841e

Browse files
authored
Add example for update / render using attr
1 parent 1c07831 commit e2a841e

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

docs/_guide/rendering.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Remember that _all_ instances of your controller _must_ add the `<template data-
5252
Sometimes you wont have a template that is server rendered, and instead want to make a template using JS. Catalyst does not support this out of the box, but it is possible to use another library: `@github/jtml`. This library can be used to write declarative templates using JS. Let's re-work the above example using `@github/jtml`:
5353

5454
```typescript
55-
import { controller, target } from "@github/catalyst"
55+
import { controller } from "@github/catalyst"
5656
import { html, render } from "@github/jtml"
5757

5858
@controller
@@ -84,3 +84,26 @@ class HelloWorldElement extends HTMLElement {
8484
```
8585

8686
Here, instead of declaring our template in HTML, we can do so in JS, and achieve exactly the same effect. We aren't using `@targets` in this example, as there is a more direct way to handle the data; re-calling `update()` will efficiently update only the parts that change.
87+
88+
The same effect could be achieved using `@attr` via:
89+
90+
```
91+
import { attr, controller } from "@github/catalyst"
92+
import { html, render } from "@github/jtml"
93+
94+
@controller
95+
class HelloWorldElement extends HTMLElement {
96+
@attr name = 'World'
97+
connectedCallback() {
98+
this.attachShadow({mode: 'open'})
99+
}
100+
101+
attributeChangedCallback() {
102+
render(() => html`
103+
<div>
104+
Hello <span>${ this.name }</span>
105+
</div>`,
106+
this.shadowRoot!)
107+
}
108+
}
109+
```

0 commit comments

Comments
 (0)