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/actions.md
+15-3Lines changed: 15 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -144,14 +144,26 @@ If you're using decorators, then the `@controller` decorator automatically handl
144
144
145
145
If you're not using decorators, then you'll need to call `bind(this)` somewhere inside of `connectedCallback()`.
146
146
147
-
```
147
+
```js
148
148
import {bind} from'@github/catalyst'
149
149
150
150
classHelloWorldElementextendsHTMLElement {
151
-
152
151
connectedCallback() {
153
152
bind(this)
154
153
}
155
-
156
154
}
157
155
```
156
+
157
+
### Binding dynamically added actions
158
+
159
+
Catalyst doesn't automatically bind actions to elements that are dynamically injected into the DOM. If you need to dynamically inject actions (for example you're injecting HTML via AJAX) you can call the `listenForBind` function to set up a observer that will bind actions when they are added to a controller.
160
+
161
+
You can provide the element you'd like to observe as a first argument and the number of items to process in a batch as a second argument. Those arguments default to `document` and `30` respectively.
162
+
163
+
Batch processing binds events in small batches to maintain UI stability (using `requestAnimationFrame` behind the scenes). We recommend the default of `30` as a sensible default, but you may find changing this number helps depending on your requirements.
Copy file name to clipboardExpand all lines: docs/_guide/conventions.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,3 +35,7 @@ Be careful not to go too short! We'd recommend avoiding contracting words such a
35
35
### Method names should describe what they do
36
36
37
37
A good method name, much like a good class name, describes what it does, not how it was invoked. While methods can be given most names, you should avoid names that conflict with existing methods on the `HTMLElement` prototype (more on that in [anti-patterns](/guide/anti-patterns#avoid-shadowing-method-names)). Names like `onClick` are best avoided, overly generic names like `toggle` should also be avoided. Just like class names it is a good idea to ask "how" and "what", so for example `showAdmins`, `filterUsers`, `updateURL`.
38
+
39
+
### `@target` should use singular naming, while `@targets` should use plural
40
+
41
+
To help differentiate the two `@target`/`@targets` decorators, the properties should be named with respective to their cardinality. That is to say, if you're using an `@target` decorator, then the name should be singular (e.g. `user`, `field`) while the `@targets` decorator should be coupled with plural property names (e.g. `users`, `fields`).
Copy file name to clipboardExpand all lines: docs/_guide/targets.md
+18-9Lines changed: 18 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ chapter: 5
3
3
subtitle: Querying Descendants
4
4
---
5
5
6
-
One of the three [core patterns](/guide/introduction#three-core-concepts-observe-listen-query) is Querying. In Catalyst, Targets are the preferred way to query. Targets use `querySelectorAll` under the hood, but in a way that makes it a lot simpler to work with.
6
+
One of the three [core patterns](/guide/introduction#three-core-concepts-observe-listen-query) is Querying. In Catalyst, Targets are the preferred way to query. Targets use `querySelector` under the hood, but in a way that makes it a lot simpler to work with.
7
7
8
8
Catalyst Components are really just Web Components, so you could simply use `querySelector` or `querySelectorAll` to select descendants of the element. Targets avoid some of the problems of `querySelector`; they provide a more consistent interface, avoid coupling CSS classes or HTML tag names to JS, and they handle subtle issues like nested components. Targets are also a little more ergonomic to reuse in a class. We'd recommend using Targets over `querySelector` wherever you can.
9
9
@@ -61,25 +61,25 @@ The target syntax follows a pattern of `controller.target`.
61
61
</span>
62
62
<divclass="p-3">
63
63
64
-
Remember! There are two decorators available, `@target` which fetches only one element, and `@targets` which fetches multiple. This is the only difference, but it's an important one.
64
+
Remember! There are two decorators available, `@target` which fetches only one `data-target`element, and `@targets` which fetches multiple`data-targets` elements!
65
65
66
66
</div>
67
67
</div>
68
68
69
-
The `@target` decorator will only ever return _one_ element, just like `querySelector`. If you want to get multiple Targets, you need the `@targets` decorator which works almost identically, but it'll return an _array_ of elements. To put this into types: `@target` returns `Element|undefined` while `@targets` returns `Array<Element>`
69
+
The `@target` decorator will only ever return _one_ element, just like `querySelector`. If you want to get multiple Targets, you need the `@targets` decorator which works almost the same, but returns an Array of elements, and it searches the `data-targets` attribute (not `data-target`).
70
70
71
71
Elements can be referenced as multiple targets, and targets may be referenced multiple times within the HTML:
0 commit comments