Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emberjs - model that changed in @each

I have an ArrayController which observes changes of a specific property of each model. I use the .observes('[email protected]') for that purpose:

App.ListController = Em.ArrayController.extend({
    content: [],

    myObserver: function () {
        //check changed property
        //do stuff if changed to some specific value
    }.observes('[email protected]')
});

So far so good - the observer fires when the property of any model changes. However, when it fires, I have no idea which model had changed - i have to iterate over models and compare that property with old values stored elsewhere to figure it out, which is very clumsy and inefficient. I tried to check the arguments that the observer receives, but they are also of no use: console.log(arguments) from within observer gives the following: [Class, "[email protected]"], and console.log(arguments[0].toString()) gives <App.ListController:ember366> - the controller itself and the property string.

So my question is: is there a way to know which model has changed when observing the array's @each property?

Update

When observer fires, I want to check whether the watched property has changed to some specific value, and if so, do some stuff.

As suggested I will check the itemController now.

like image 710
Shimon Rachlenko Avatar asked Nov 12 '22 10:11

Shimon Rachlenko


1 Answers

You can achieve this in current ember using reduceComputed or arrayComputed.

It would be good to know your use case though: very likely there is a better approach than manually creating an observer.

like image 189
hjdivad Avatar answered Nov 15 '22 01:11

hjdivad