Skip to content

Instantly share code, notes, and snippets.

@sukima
Last active June 25, 2019 20:54
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 sukima/1452f330b28d73a3d27bcc5ecc324a58 to your computer and use it in GitHub Desktop.
Save sukima/1452f330b28d73a3d27bcc5ecc324a58 to your computer and use it in GitHub Desktop.
Component Lifecycles
import Ember from 'ember';
export default Ember.Component.extend({
logger: Ember.inject.service(),
log(message) {
this.get('logger').log(this.get('name'), message);
},
init() {
this._super(...arguments);
this.log('init');
},
didInsertElement() {
this._super(...arguments);
this.log('didInsertElement');
},
// This is not a thing
willRemoveElement() {
this._super(...arguments);
this.log('willRemoveElement');
},
// This is not a thing
didRemoveElement() {
this._super(...arguments);
this.log('didRemoveElement');
},
willDestroyElement() {
this._super(...arguments);
this.log('willDestroyElement');
},
didDestroyElement() {
this._super(...arguments);
this.log('didDestroyElement');
},
willDestroy() {
this._super(...arguments);
this.log('willDestroy');
},
didDestroy() {
this._super(...arguments);
this.log('didDestroy');
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
logger: Ember.inject.service()
});
import EmberRouter from '@ember/routing/router';
import config from './config/environment';
const Router = EmberRouter.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('foo');
this.route('bar');
});
export default Router;
import Ember from 'ember';
export default Ember.Service.extend({
init() {
this._super(...arguments);
this.set('logs', []);
},
log(label, message) {
this.get('logs').pushObject({ label, message });
}
});
{{link-to "Foo" "foo"}} | {{link-to "Bar" "bar"}}
<br>
{{outlet}}
<ol>
{{#each logger.logs as |log|}}
<li>{{log.label}} -> {{log.message}}</li>
{{/each}}
</ol>
<h1>Bar</h1>
{{#my-component name="Bar"}}
{{my-component name="Bar Child"}}
{{/my-component}}
<h3>my-component ({{name}})</h3>
{{yield}}
<h1>Foo</h1>
{{my-component name="Foo"}}
<p>Switch routes to see components being rendered and removed</p>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0"
},
"addons": {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment