user1158559
user1158559

Reputation: 1954

Destroying objects in Ember.js

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?

http://jsfiddle.net/FcsRP/

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.

Upvotes: 5

Views: 5602

Answers (1)

Roy Daniels
Roy Daniels

Reputation: 6309

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('[email protected]')
});
a2 = Ember.ArrayController.create({
    content: [],
    destroyedObj: function() {
        alert('destroyed obj observer in a2');
  }.observes('[email protected]')
});

a1.pushObject(obj);
a1.pushObject(obj);
a2.pushObject(obj);

obj.destroy()

alert(a1.get('content').length)

Upvotes: 7

Related Questions