Skip to content

Instantly share code, notes, and snippets.

@rafaltrojanowski
Last active September 26, 2018 04:46
Show Gist options
  • Save rafaltrojanowski/9752bc936d36f2de0a5338abb8e092fb to your computer and use it in GitHub Desktop.
Save rafaltrojanowski/9752bc936d36f2de0a5338abb8e092fb to your computer and use it in GitHub Desktop.
component-refractor.js
import Component from '@ember/component'; // modern syntax
export default Component.extend({ // redundant Ember, that has been already imported.
count: 1, // it will start from 2, imho it should be 0
// Use didInsertElement hook instead of init.
// I assume that we want to start updating counter begining from the moment when the element was inserted to the DOM.
// `init` is called before didInsertElement() and would lead to some
// init() does several things behind the scenes.
// `init` hook would require calling `this._super(...arguments);`
// the only use-case when I use init hook is to set Component related state like internal errors:
// Example:
// init() {
// this._super(...arguments);
// this.set('errors', []);
// }
// Error: Assertion Failed: You must call `this._supe
didInsertElement() {
this.updateCounter();
},
updateCounter() {
this.incrementProperty('count')
Ember.run.later(this, this.updateCounter, 1000)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment