Commit 04f5d5c
committed
Adds actions to components
Actions add additional functionality to elements within your component's template that may be difficult to add with other mechanisms. Examples of functionality which actions makes trivial to attach are:
* tooltips
* image lazy loaders
* drag and drop functionality
Actions can be added to an element with the `use` directive.
```html
<img use:lazyload data-src="giant-photo.jpg>
```
Data may be passed to the action as an object literal (e.g. `use:b="{ setting: true }"`, a literal value (e.g. `use:b="'a string'"`), or a value or function from your component's state (e.g. `add:b="foo"` or `add:b="foo()"`).
Actions are defined in a "actions" property on your component definition.
```html
<script>
export default {
actions: {
b(node, data) {
// do something
return {
update(data) {},
destroy() {}
}
}
}
}
</script>
```
A action is a function which receives a reference to an element and optionally the data if it is added in the HTML. This function can then attach listeners or alter the element as needed. The action can optionally return an object with the methods `update(data)` and `destroy()`.
When data is added in the HTML and comes from state, the action's `update(data)` will be called if defined whenever the state is changed.
When the element is removed from the DOM `destroy()` will be called if provided, allowing for cleanup of event listeners, etc.
See #469 for discussion around this feature and more examples of how it could be used.1 parent bc416a5 commit 04f5d5c
File tree
33 files changed
+1025
-132
lines changed- src
- generators
- nodes
- parse/read
- validate
- html
- js
- propValidators
- test
- js/samples/action
- parser
- samples
- action-with-call
- action-with-identifier
- action-with-literal
- action
- runtime/samples
- action-function
- action-update
- action
- trait-function
- validator/samples
- action-invalid
- action-on-component
33 files changed
+1025
-132
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| 94 | + | |
94 | 95 | | |
95 | 96 | | |
96 | 97 | | |
| |||
134 | 135 | | |
135 | 136 | | |
136 | 137 | | |
| 138 | + | |
137 | 139 | | |
138 | 140 | | |
139 | 141 | | |
| |||
452 | 454 | | |
453 | 455 | | |
454 | 456 | | |
455 | | - | |
| 457 | + | |
456 | 458 | | |
457 | 459 | | |
458 | 460 | | |
| |||
636 | 638 | | |
637 | 639 | | |
638 | 640 | | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
639 | 647 | | |
640 | 648 | | |
641 | 649 | | |
| |||
824 | 832 | | |
825 | 833 | | |
826 | 834 | | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
827 | 845 | | |
828 | 846 | | |
829 | 847 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
0 commit comments