Skip to content

Commit 48faeb4

Browse files
authored
Merge pull request #8 from GavinJoyce/gj/api-docs
`{{did-insert}}`, `{{did-update}}` and `{{will-destroy}}` API docs
2 parents 3180d09 + 17aa8a1 commit 48faeb4

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

addon/modifiers/did-insert.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
import Ember from 'ember';
22

3+
/**
4+
The `{{did-insert}}` element modifier is activated when an element is
5+
inserted into the DOM.
6+
7+
In this example, the `fadeIn` function receives the `div` DOM element as its
8+
first argument and is executed after the element is inserted into the DOM.
9+
10+
```handlebars
11+
<div {{did-insert this.fadeIn}} class="alert">
12+
{{yield}}
13+
</div>
14+
```
15+
16+
```js
17+
export default Component.extend({
18+
fadeIn(element) {
19+
element.classList.add('fade-in');
20+
}
21+
});
22+
```
23+
24+
By default, the executed function will be unbound. If you would like to access
25+
the component context in your function, use the `action` helper as follows:
26+
27+
```handlebars
28+
<div {{did-insert (action this.incrementCount)}}>first</div>
29+
<div {{did-insert (action this.incrementCount)}}>second</div>
30+
31+
<p>{{this.count}} elements were rendered</p>
32+
```
33+
34+
```js
35+
export default Component.extend({
36+
@tracked count = 0;
37+
38+
incrementCount() {
39+
this.count++;
40+
}
41+
});
42+
```
43+
44+
@method did-insert
45+
@public
46+
*/
347
export default Ember._setModifierManager(
448
() => ({
549
createModifier() {},

addon/modifiers/did-update.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
import Ember from 'ember';
22

3+
/**
4+
The `{{did-update}}` element modifier is activated when any of its arguments
5+
are updated. It does not run on initial render.
6+
7+
In this example, the `resize` function receives the `textarea` DOM element as its
8+
first argument and is executed anytime the `@text` argument changes.
9+
10+
```handlebars
11+
<textarea {{did-update this.resize @text}} readonly style="padding: 0px;">
12+
{{@text}}
13+
</textarea>
14+
```
15+
16+
```js
17+
export default Component.extend({
18+
resize(element) {
19+
element.style.height = `${element.scrollHeight}px`;
20+
}
21+
});
22+
```
23+
24+
In addition to the `element`, both named and positional arguments are passed to the
25+
executed function:
26+
27+
```handlebars
28+
<div {{did-update this.logArguments @first @second third=@third}} />
29+
```
30+
31+
```js
32+
export default Component.extend({
33+
logArguments(element, [first, second], { third }) {
34+
console.log('element', element);
35+
console.log('positional args', first, second);
36+
console.log('names args', third);
37+
}
38+
});
39+
```
40+
41+
By default, the executed function will be unbound. If you would like to access
42+
the component context in your function, use the `action` helper as follows:
43+
44+
```handlebars
45+
<div {{did-update (action this.someFunction) @someArg} />
46+
```
47+
48+
@method did-update
49+
@public
50+
*/
351
export default Ember._setModifierManager(
452
() => ({
553
createModifier() {

addon/modifiers/will-destroy.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
import Ember from 'ember';
22

3+
/**
4+
The `{{will-destroy}}` element modifier is activated immediately before the element
5+
is removed from the DOM.
6+
7+
```handlebars
8+
<div {{will-destroy this.teardownPlugin}}>
9+
{{yield}}
10+
</div>
11+
```
12+
13+
```js
14+
export default Component.extend({
15+
teardownPlugin(element) {
16+
// teardown logic here
17+
}
18+
});
19+
```
20+
21+
By default, the executed function will be unbound. If you would like to access
22+
the component context in your function, use the `action` helper as follows:
23+
24+
```handlebars
25+
<div {{will-destroy (action this.teardownPlugin)}}>
26+
{{yield}}
27+
</div>
28+
```
29+
30+
@method will-destroy
31+
@public
32+
*/
333
export default Ember._setModifierManager(
434
() => ({
535
createModifier() {

0 commit comments

Comments
 (0)