Skip to content

Instantly share code, notes, and snippets.

@jerel
Last active January 28, 2016 19:08
Show Gist options
  • Save jerel/1523fd2fae4a21c261d7 to your computer and use it in GitHub Desktop.
Save jerel/1523fd2fae4a21c261d7 to your computer and use it in GitHub Desktop.
Ember unloadRecord doesn't remove observers
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle'
});
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel: function() {
this.store.push({
data: {
type: 'event',
id: 1,
attributes: {
name: 'test',
}
}
});
},
model: function() {
return this.store.peekAll('event');
},
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
A model should show here for 5 seconds and then disappear. However even after the model is destroyed the observer stays attached as you can see in the console:
<br>
<br>
{{#each model as |item|}}
{{item.name}}, dies at age {{item.ticks}} seconds
{{/each}}
<br>
<br>
import Ember from 'ember';
export default Ember.Service.extend({
init: function() {
var self = this;
console.log('clock started');
window.setInterval(function() {
self.incrementProperty('tick');
}, 1000);
},
});
import DS from 'ember-data';
export default DS.Model.extend({
clock: Ember.inject.service(),
name: DS.attr(),
ticks: Ember.computed('clock.tick', function() {
return this.get('clock.tick');
}),
// this is a way of removing stale models after a certain time period has passed
aging: Ember.observer('clock.tick', function() {
console.log(this.get('clock.tick'));
if (this.get('clock.tick') > 5) {
// manually removing observer fixes the issue
// this.removeObserver('clock.tick');
this.unloadRecord(this);
console.log('record unloaded, observer should stop now');
}
}),
});
{
"version": "0.5.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "canary",
"ember-data": "canary",
"ember-template-compiler": "canary"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment