You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/_guide/rendering.md
+22-22Lines changed: 22 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,58 +51,58 @@ Remember that _all_ instances of your controller _must_ add the `<template data-
51
51
52
52
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`:
53
53
54
-
```typescript
55
-
import { controller } from"@github/catalyst"
54
+
```
55
+
import { attr, controller } from "@github/catalyst"
56
56
import { html, render } from "@github/jtml"
57
57
58
58
@controller
59
59
class HelloWorldElement extends HTMLElement {
60
-
61
-
// Make `name` automatically update when changed
62
-
#name ='World'
63
-
get name() {
64
-
returnthis.#name
65
-
}
66
-
set name(value:string) {
67
-
this.#name=value
68
-
this.update()
69
-
}
60
+
@attr name = 'World'
70
61
71
62
connectedCallback() {
72
63
this.attachShadow({mode: 'open'})
73
-
this.update()
74
64
}
75
65
76
-
update() {
66
+
attributeChangedCallback() {
77
67
render(() => html`
78
68
<div>
79
-
Hello <span>${ this.#name }</span>
69
+
Hello <span>${ this.name }</span>
80
70
</div>`,
81
71
this.shadowRoot!)
82
72
}
83
73
}
84
74
```
85
75
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.
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.
87
77
88
-
The same effect could be achieved using `@attr` via:
78
+
The same effect could be achieved without using `@attr` via:
89
79
90
-
```
91
-
import { attr, controller } from "@github/catalyst"
0 commit comments