Oliver Zheng
Oliver Zheng

Reputation: 8199

How can I observe array changes and see which new element is added?

onArrayChanged: function(obj, keyName, value) {

    // What is value here, exactly?

}.property('array.@each')

When an element is added to the array, how do I know which value was added? LIkewise, when a value is removed from the array, how do I access that?

Upvotes: 5

Views: 2615

Answers (1)

pangratz
pangratz

Reputation: 16143

Have a look at addArrayObserver, see http://jsfiddle.net/pangratz666/EE65Z/:

var a = Ember.A('a b c d e f g'.w());

var o = Ember.Object.create({
    arrayWillChange: Ember.K,
    arrayDidChange: function(array, start, removeCount, addCount) {
        console.log(arguments);
    }
});

a.addArrayObserver(o);

Upvotes: 12

Related Questions