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

Deadly 提交于 2019-12-18 17:06:48

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/10390050/how-can-i-observe-array-changes-and-see-which-new-element-is-added

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!