Skip to content

Instantly share code, notes, and snippets.

@grapho
Last active November 19, 2015 18:10
Show Gist options
  • Save grapho/3662e7fe8a29d2b81010 to your computer and use it in GitHub Desktop.
Save grapho/3662e7fe8a29d2b81010 to your computer and use it in GitHub Desktop.
Component Life Cycles
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Component Life-cycle Hooks',
myAttr: "Red"
});
<h1>{{appName}}</h1>
{{life-cycles
myAttr=myAttr
setAttr=(action (mut myAttr))}}
import Ember from 'ember';
export default Ember.Component.extend({
myProp: null,
/**
@didInitAttrs()
Runs after a component was created and passed attrs are guaranteed to be present. In Ember 1.13, the attributes will be available as this.get('attrName').
- Runs 1st on initial render
- Setting internal component properties in this hook is ok (does not trigger warning/deprecation)
*/
didInitAttrs() {
console.log('didInitAttrs');
},
/**
@didUpdateAttrs()
runs when the attributes of a component have changed (but not when the component is re-rendered, via component.rerender, component.set, or changes in models or services used by the template).
- Runs 1st on re-renders, when an external attr has changed
- Does not Run when an internal property is set()
- Setting properties in this hook is ok (does not trigger warning/deprecation)
*/
didUpdateAttrs() {
console.log('didUpdateAttrs');
},
/**
@didReceiveAttrs()
- Runs 2nd after didInitAttrs, on initial render
- Runs 2nd on re-renders, when an external attr has changed
- Does not Run when an internal property is set()
- Setting properties in this hook is ok (does not trigger warning/deprecation)
*/
didReceiveAttrs() {
console.log('didReceiveAttrs');
},
/**
@didInsertElement()
runs after the template has rendered and the element is in the DOM.
- Runs 4th only on initial render
- Setting properties in this hook which would result in a template re-render is NOT ok (triggers warning/deprecation)
*/
didInsertElement() {
console.log('didInsertElement');
},
/**
@willUpdate()
runs when the component is re-rendering for any reason, including component.rerender(), component.set() or changes in models or services used by the template.
- Runs 3rd, on re-renders, when an external attr has changed
- Runs 1st, on re-renders, when an internal prop has changed
- Setting properties in this hook is ok (does not trigger warning/deprecation)
*/
willUpdate() {
console.log('willUpdate');
},
/**
@willRender()
Runs before the template is rendered. It runs when the template is updated for any reason (both initial and re-render, and regardless of whether the change was caused by an attrs change or re-render).
- Runs 3rd on initial render
- Runs 4th on re-renders, when an external attr has changed
- Runs 2nd on re-renders, when an internal prop has changed
- Setting properties in this hook is ok (does not trigger warning/deprecation)
*/
willRender() {
console.log('willRender');
},
/**
@didUpdate()
runs after the template has re-rendered and the DOM is now up to date.
- Runs 5th on re-renders, when an external attr has changed
- Runs 3rd on re-renders, when an internal prop has changed
- Setting properties in this hook which would result in a template re-render is NOT ok (triggers warning/deprecation)
*/
didUpdate() {
console.log('didUpdate');
},
/**
@didRender()
runs after didInsertElement (it also runs on subsequent re-renders).
- Always runs last
- Setting properties in this hook which would result in a template re-render is NOT ok (triggers warning/deprecation)
*/
didRender() {
console.log('didRender');
console.log('----------------------');
},
});
<p>External Attr: {{myAttr}}</p>
<p>Internal Prop: {{myProp}}</p>
<div>
<button onclick={{action setAttr "Blue" on="click"}}>Set Attr</button>
<button onclick={{action (mut myProp) "Green"}}>Set Prop</button>
</div>
{
"version": "0.4.16",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.1.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment