Destroying objects in Ember.js
Asked Answered
I

1

5

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.

Incensory answered 29/4, 2012 at 22:39 Comment(0)
P
7

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)
Paulin answered 5/5, 2012 at 2:40 Comment(1)
Hi Roy, don't know why I didn't accept your answer at the time, but it 's definitely correct! Thankyou!Incensory

© 2022 - 2024 — McMap. All rights reserved.