forked from emberjs/ember-render-modifiers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdid-update.js
93 lines (78 loc) · 2.65 KB
/
did-update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { setModifierManager, capabilities } from '@ember/modifier';
import { untrack } from '@glimmer/validator';
/**
The `{{did-update}}` element modifier is activated when any of its arguments
are updated. It does not run on initial render.
In this example, the `resize` function receives the `textarea` DOM element as its
first argument and is executed anytime the `@text` argument changes.
```handlebars
<textarea {{did-update this.resize @text}} readonly style="padding: 0px;">
{{@text}}
</textarea>
```
```js
export default Component.extend({
resize(element) {
element.style.height = `${element.scrollHeight}px`;
}
});
```
In addition to the `element`, both named and positional arguments are passed to the
executed function:
```handlebars
<div {{did-update this.logArguments @first @second third=@third}} />
```
```js
export default Component.extend({
logArguments(element, [first, second], { third }) {
console.log('element', element);
console.log('positional args', first, second);
console.log('names args', third);
}
});
```
By default, the executed function will be unbound. If you would like to access
the component context in your function, use the `action` decorator as follows:
```handlebars
<div {{did-update this.someFunction @someArg} />
```
```js
export default Component.extend({
someFunction: action(function(element, [someArg]) {
// the `this` context will be the component instance
})
});
```
@method did-update
@public
*/
export default setModifierManager(
() => ({
capabilities: capabilities('3.22', { disableAutoTracking: false }),
createModifier() {
return { element: null };
},
installModifier(state, element, args) {
// save element into state bucket
state.element = element;
// Consume individual properties to entangle tracking.
// https://github.com/emberjs/ember.js/issues/19277
// https://github.com/ember-modifier/ember-modifier/pull/63#issuecomment-815908201
args.positional.forEach(() => {});
args.named && Object.values(args.named);
},
updateModifier({ element }, args) {
let [fn, ...positional] = args.positional;
// Consume individual properties to entangle tracking.
// https://github.com/emberjs/ember.js/issues/19277
// https://github.com/ember-modifier/ember-modifier/pull/63#issuecomment-815908201
args.positional.forEach(() => {});
args.named && Object.values(args.named);
untrack(() => {
fn(element, positional, args.named);
});
},
destroyModifier() {},
}),
class DidUpdateModifier {},
);