0
votes

I try to observe (in my controller) if my Ember model has changed.

personChanged: function() {
    // do stuff
}.observes('person.dirtyType'),

This observer is never triggerd unless I will access the isDirty property before. For example if I get the property in the route (where the model is fetched) the observer is triggerd exactly 1 time.

model.people.get('firstObject').get('dirtyType');
controller.set('person', model.people.get('firstObject'));

If I want to get the observer triggered every time the model changed I need to access dirtyType within the observer again.

personChanged: function() {
    this.get('person.dirtyType');
    // do stuff
}.observes('person.dirtyType'),

The value of dirtyType in the observer is always as expected.

Maybe I'm doing it completely wrong but I can't follow the behavior above.

1

1 Answers

0
votes

There is something unpredictable is going on when we use firstObject based on this question Ember computed alias on array firstObject not working

I haven't experienced it to confirm. May be until then you can try the below workaround,

controller.set('person', model.people.get('firstObject'));

Instead of the above, you can define computed property,

person:Ember.computed('model.people.[]',function(){
 return return this.get('model.people.firstObject');
})

Now your below observer will work all the time.

personChanged:Ember.observer('person.dirtyType',function() {
    // do stuff
}),