-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdid-insert.js
More file actions
74 lines (60 loc) · 1.69 KB
/
did-insert.js
File metadata and controls
74 lines (60 loc) · 1.69 KB
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
import { assert } from '@ember/debug';
import { setModifierManager, capabilities } from '@ember/modifier';
/**
The `{{did-insert}}` element modifier is activated when an element is
inserted into the DOM.
In this example, the `fadeIn` function receives the `div` DOM element as its
first argument and is executed after the element is inserted into the DOM.
```handlebars
<div {{did-insert this.fadeIn}} class="alert">
{{yield}}
</div>
```
```js
export default Component.extend({
fadeIn(element) {
element.classList.add('fade-in');
}
});
```
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-insert this.incrementCount}}>first</div>
<div {{did-insert this.incrementCount}}>second</div>
<p>{{this.count}} elements were rendered</p>
```
```js
export default Component.extend({
count: tracked({ value: 0 }),
incrementCount: action(function() {
this.count++;
})
});
```
@method did-insert
@public
*/
export default setModifierManager(
() => ({
capabilities: capabilities('3.13', { disableAutoTracking: true }),
createModifier() {},
installModifier(
_state,
element,
{
positional: [didInsertFunction, ...args],
named,
}
) {
assert(
`You need to pass a function as the first argument to \`did-insert\` you passed ${didInsertFunction}`,
typeof didInsertFunction === 'function'
);
didInsertFunction(element, args, named);
},
updateModifier() {},
destroyModifier() {},
}),
class DidInsertModifier {}
);