Imagine I create an ember object, then add it to an arbitrary unknown number of array controllers. Is there a simple way of destroying the object so that all the array controllers get notified and remove it?
destroy from Ember.CoreObject doesn't seem to notify the collections that their objects have been destroyed, or the collections don't remove their objects. I'm not even sure if they're meant to or not.
The easiest way that I can think of is adding an observer on the object's isDestroyed
property. That way when you destroy something and that property becomes true
you can run whatever code you need to.
See this jsfiddle: http://jsfiddle.net/ud3323/FSCyF/
Code:
obj = Ember.Object.create({});
a1 = Ember.ArrayController.create({
content: [],
destroyedObj: function() {
alert('destroyed obj observer in a1');
}.observes('content.@each.isDestroyed')
});
a2 = Ember.ArrayController.create({
content: [],
destroyedObj: function() {
alert('destroyed obj observer in a2');
}.observes('content.@each.isDestroyed')
});
a1.pushObject(obj);
a1.pushObject(obj);
a2.pushObject(obj);
obj.destroy()
alert(a1.get('content').length)