0

I've some troubles with the observer. I would like to observe some model properties but somehow the observer is triggered on initialisation also. I only want to trigger the function when a user is really changing one of those properties.

The observer looks something like this:

  watchProperties: function(){
    console.log('Changed :D');
  }.observes('model.propertyOne', 'model.propertyTwo', 'model.propertyThree')

The modelhook is set in another route with modelFor and the model is serialised embedded JSON data. Is it possible that one of these things causes the issue?

1 Answers1

0

This is expected behaviour (http://emberjs.com/api/classes/Ember.Object.html#method_addObserver). If you wish to trigger the function only when a property really changes, then you have two options:

Community
  • 1
  • 1
Thomas Brus
  • 931
  • 5
  • 11
  • Thanks Thomas. Indeed both options aren't ideal. I think in my case it's easier to observe when the inputs of the properties change. But still, there should be a specific Ember function to deal with this. – Alvin Vogelzang Nov 11 '14 at 22:14
  • I agree ;) By the way, you might even be able to get away with `model.changedAttributes()` to check which properties are changed & not yet persisted – Thomas Brus Nov 11 '14 at 23:18
  • Hmm I found another way to only observe changes after initialization. I used the addObserver method to set the observer in the next runloop. `init: function(){ this._super(); var self = this; Ember.run.later(function(){ self.addObserver('model.propertyOne', self.watchProperties); self.addObserver('model.propertyOne', self.watchProperties); self.addObserver('model.propertyOne', self.watchProperties); }); }, watchProperties: function(){ this.set('propertiesDidChange', true); },` – Alvin Vogelzang Nov 12 '14 at 10:14