Skip to content

Commit 6c86d59

Browse files
authored
Merge pull request #124 from github/srt32-patch-1
Add example for update / render using attr
2 parents 1c07831 + 4e7bceb commit 6c86d59

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

docs/_guide/rendering.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,33 @@ 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 { attr, controller } from "@github/catalyst"
56+
import { html, render } from "@github/jtml"
57+
58+
@controller
59+
class HelloWorldElement extends HTMLElement {
60+
@attr name = 'World'
61+
62+
connectedCallback() {
63+
this.attachShadow({mode: 'open'})
64+
}
65+
66+
attributeChangedCallback() {
67+
render(() => html`
68+
<div>
69+
Hello <span>${ this.name }</span>
70+
</div>`,
71+
this.shadowRoot!)
72+
}
73+
}
74+
```
75+
76+
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; relying on `attributeChangedCallback` which will efficiently update only the parts that change.
77+
78+
The same effect could be achieved without using `@attr` via:
79+
80+
```typescript
81+
import { controller } from "@github/catalyst"
5682
import { html, render } from "@github/jtml"
5783

5884
@controller
@@ -82,5 +108,3 @@ class HelloWorldElement extends HTMLElement {
82108
}
83109
}
84110
```
85-
86-
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.

0 commit comments

Comments
 (0)