Sunny Singh
Sunny Singh

Reputation: 57

Ember.js Array observer

I have the following observer:

testArrayObserver: function(){
....

}.observes('testArray.@each'),

Now, this observer will hit whenever any changes are made to array may an element gets added or removed.

I want to asked that when ever this observer hits, can we know the reason i.e. weather adding element to array invoked the observer or removing an element from the array invoked the observer.

Upvotes: 1

Views: 2008

Answers (1)

Daniel
Daniel

Reputation: 18680

I want to asked that when ever this observer hits, can we know the reason i.e. weather adding element to array invoked the observer or removing an element from the array invoked the observer.

No, we can't. Ember hasn't got any public API which you could use to get this information. You could however create your own logic to keep track last length of array and current length of this array. Based on comparison of these 2 numbers you could figure out whether element has been added or removed.

And using testArray.@each is deprecated. You'd better use:

testArrayObserver: Ember.observer('testArray.[]', function() {
  // ...
}),

Upvotes: 2

Related Questions