Skip to content

Instantly share code, notes, and snippets.

@kybishop
Last active January 2, 2017 17:58
Show Gist options
  • Save kybishop/3ca76407c810fb0c6b066c638cc2e1e8 to your computer and use it in GitHub Desktop.
Save kybishop/3ca76407c810fb0c6b066c638cc2e1e8 to your computer and use it in GitHub Desktop.
Can't cancel task
import Ember from 'ember';
export default Ember.Component.extend({
didInsertElement() {
this._super(...arguments);
console.log('in didInsertElement');
console.log("element: ");
console.log(this.get('element'));
let afterRenderHandler = Ember.run.schedule('afterRender', () => {
console.log('in afterRender');
console.log("is destroying: " + this.get('isDestroying'));
console.log("is destroyed: " + this.get('isDestroyed'));
console.log("element in afterRender: ");
console.log(this.get('element'));
});
this.set('afterRenderHandler', afterRenderHandler);
},
willDestroyElement() {
this._super(...arguments);
console.log('in willDestroyElement');
console.log('element in willDestroyElement');
console.log(this.get('element'));
Ember.run.cancel(this.get('afterRenderHandler'));
}
});
import Ember from 'ember';
export default Ember.Component.extend({
sharedService: Ember.inject.service(),
didInsertElement() {
this._super(...arguments);
console.log('mutating shared service');
this.set('sharedService.showDestroyedComponent', false);
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
sharedService: Ember.inject.service(),
actions: {
toggleTest() {
this.toggleProperty('sharedService.show');
this.toggleProperty('sharedService.showDestroyedComponent');
}
}
});
import Ember from 'ember';
export default Ember.Service.extend({
show: false,
});
<button {{action "toggleTest"}}>{{if sharedService.show "Tear down test" "Run test"}}</button>
<br>
{{#if sharedService.show}}
{{#if sharedService.showDestroyedComponent}}
{{destroyed-component}}
{{/if}}
{{service-mutating-component}}
{{/if}}
<p>destroyed component template</p>
<p>service mutating component template</p>
{
"version": "0.10.7",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.5.1",
"ember-template-compiler": "2.5.1",
"ember-testing": "2.5.1"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment