Skip to content

Instantly share code, notes, and snippets.

@Subtletree
Last active January 20, 2021 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Subtletree/1e33184eeaf1f5e745a8d3940026f958 to your computer and use it in GitHub Desktop.
Save Subtletree/1e33184eeaf1f5e745a8d3940026f958 to your computer and use it in GitHub Desktop.
Nested component hooks
import Ember from 'ember';
export default Ember.Component.extend({
number: 0,
actions: {
inc() {
this.incrementProperty('number');
}
},
init() {
this._super(...arguments);
console.log('%c Child: init', 'color: blue;');
},
didUpdateAttrs() {
this._super(...arguments);
console.log('%c Child: didUpdateAttrs', 'color: blue;');
},
didReceiveAttrs() {
this._super(...arguments);
console.log('%c Child: didReceiveAttrs', 'color: blue;');
},
willUpdate() {
this._super(...arguments);
console.log('%c Child: willUpdate', 'color: blue;');
},
willRender() {
this._super(...arguments);
console.log('%c Child: willRender', 'color: blue;');
},
didInsertElement() {
this._super(...arguments);
console.log('%c Child: didInsertElement', 'color: blue;');
},
didUpdate() {
this._super(...arguments);
console.log('%c Child: didUpdate', 'color: blue;');
},
didRender() {
this._super(...arguments);
console.log('%c Child: didRender', 'color: blue;');
}
});
import Ember from 'ember';
export default Ember.Component.extend({
number: 0,
actions: {
inc() {
this.incrementProperty('number');
}
},
init() {
this._super(...arguments);
console.log('%c Grandchild: init', 'color: orange;');
},
didUpdateAttrs() {
this._super(...arguments);
console.log('%c Grandchild: didUpdateAttrs', 'color: orange;');
},
didReceiveAttrs() {
this._super(...arguments);
console.log('%c Grandchild: didReceiveAttrs', 'color: orange;');
},
willUpdate() {
this._super(...arguments);
console.log('%c Grandchild: willUpdate', 'color: orange;');
},
willRender() {
this._super(...arguments);
console.log('%c Grandchild: willRender', 'color: orange;');
},
didInsertElement() {
this._super(...arguments);
console.log('%c Grandchild: didInsertElement', 'color: orange;');
},
didUpdate() {
this._super(...arguments);
console.log('%c Grandchild: didUpdate', 'color: orange;');
},
didRender() {
this._super(...arguments);
console.log('%c Grandchild: didRender', 'color: orange;');
}
});
import Ember from 'ember';
export default Ember.Component.extend({
number: 0,
showChild: true,
actions: {
inc() {
this.incrementProperty('number');
}
},
init() {
this._super(...arguments);
console.log('%c Parent: init', 'color: green;');
},
didUpdateAttrs() {
this._super(...arguments);
console.log('%c Parent: didUpdateAttrs', 'color: green;');
},
didReceiveAttrs() {
this._super(...arguments);
console.log('%c Parent: didReceiveAttrs', 'color: green;');
},
willUpdate() {
this._super(...arguments);
console.log('%c Parent: willUpdate', 'color: green;');
},
willRender() {
this._super(...arguments);
console.log('%c Parent: willRender', 'color: green;');
},
didInsertElement() {
this._super(...arguments);
console.log('%c Parent: didInsertElement', 'color: green;');
},
didUpdate() {
this._super(...arguments);
console.log('%c Parent: didUpdate', 'color: green;');
},
didRender() {
this._super(...arguments);
console.log('%c Parent: didRender', 'color: green;');
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Nested component hooks'
});
{{outlet}}
<h4>Open console</h4>
{{#parent-component}}
{{#if this.showChild}}
{{#child-component}}
{{grand-child-component}}
{{/child-component}}
{{/if}}
{{/parent-component}}
{{log '%c Child: Template start' 'color: blue;'}}
<button {{action 'inc'}}>Re render child: {{number}}</button>
{{yield}}
{{log '%c Child: Template end' 'color: blue;'}}
{{log '%c Grandchild: Template start' 'color: orange;'}}
<button {{action 'inc'}}>Re render grandchild: {{number}}</button>
{{yield}}
{{log '%c Grandchild: Template end' 'color: orange;'}}
{{log '%c Parent: Template start' 'color: green;'}}
<button {{action 'inc'}}>Re render parent: {{number}}</button>
{{yield}}
{{log '%c Parent: Template end' 'color: green;'}}
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment