Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 760
user1158559 Avatar asked Apr 29 '12 22:04

user1158559


1 Answers

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)
like image 128
Roy Daniels Avatar answered Nov 01 '22 10:11

Roy Daniels