Skip to content

Commit 57f15d4

Browse files
authored
Reorder
1 parent ae64b89 commit 57f15d4

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

docs/_guide/rendering.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,58 +51,58 @@ Remember that _all_ instances of your controller _must_ add the `<template data-
5151

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

54-
```typescript
55-
import { controller } from "@github/catalyst"
54+
```
55+
import { attr, controller } from "@github/catalyst"
5656
import { html, render } from "@github/jtml"
5757
5858
@controller
5959
class HelloWorldElement extends HTMLElement {
60-
61-
// Make `name` automatically update when changed
62-
#name = 'World'
63-
get name() {
64-
return this.#name
65-
}
66-
set name(value: string) {
67-
this.#name = value
68-
this.update()
69-
}
60+
@attr name = 'World'
7061
7162
connectedCallback() {
7263
this.attachShadow({mode: 'open'})
73-
this.update()
7464
}
7565
76-
update() {
66+
attributeChangedCallback() {
7767
render(() => html`
7868
<div>
79-
Hello <span>${ this.#name }</span>
69+
Hello <span>${ this.name }</span>
8070
</div>`,
8171
this.shadowRoot!)
8272
}
8373
}
8474
```
8575

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.
8777

88-
The same effect could be achieved using `@attr` via:
78+
The same effect could be achieved without using `@attr` via:
8979

90-
```
91-
import { attr, controller } from "@github/catalyst"
80+
```typescript
81+
import { controller } from "@github/catalyst"
9282
import { html, render } from "@github/jtml"
9383

9484
@controller
9585
class HelloWorldElement extends HTMLElement {
96-
@attr name = 'World'
86+
87+
// Make `name` automatically update when changed
88+
#name = 'World'
89+
get name() {
90+
return this.#name
91+
}
92+
set name(value: string) {
93+
this.#name = value
94+
this.update()
95+
}
9796

9897
connectedCallback() {
9998
this.attachShadow({mode: 'open'})
99+
this.update()
100100
}
101101

102-
attributeChangedCallback() {
102+
update() {
103103
render(() => html`
104104
<div>
105-
Hello <span>${ this.name }</span>
105+
Hello <span>${ this.#name }</span>
106106
</div>`,
107107
this.shadowRoot!)
108108
}

0 commit comments

Comments
 (0)