Ember Multiple Property Observer


#1

Hi everyone, I have an observer that observes multiple properties say ‘X’, ‘Y’ & ‘Z’. Is there any way to find that when a property is fired which exact property was changed?


#2

As far as I know: no, there isn’t any. You could simply register an observer for each property and move shared logic to a function which is executed by these observers. Just have in mind that in these case multiple observes may be fired in same run loop. This will be the case if more than one of the properties changes in that specific run loop.


#3

To use multiples observers and avoid multiple runs in same run loop is good to use the run.once.

Ember.observer('a', function() {  Ember.run.once(this,'myMethod') })
Ember.observer('b', function() {  Ember.run.once(this,'myMethod') })
Ember.observer('c', function() {  Ember.run.once(this,'myMethod') })

#4

It’s not only about using run.once() but also about passing the information which properties have been changed to the method. Otherwise you can’t tell in method executed, which property has changed. So you would end up with a code complex as:

EmberObject.extend({
  myMethod() {
    let changedProperties = this.get('changedProperties');

    // do your stuff

    this.set('changedProperties', []);
  },

  init() {
    this.set('changedProperties', []);
    let properties = ['a', 'b', 'c'];
    properties.forEach((property) => {
      this.addObserver(() => {
        this.get('changedProperties').push(property);
        once(this, 'myMethod');
      });
    });
  }
});

Or to put it in other words: You should avoid such code. Don’t use observers if there is any other possibility. If you have to use observers: Don’t rely on the exact property being changed if you observe multiple properties. There is a better approach in nearly all cases.

Disclaimer: I’m not quite sure if the optional args argument might help to get an easier approach working. Would be interesting how once behaves if executed in same run loop for same target and method but with different args. Api docs are not that clear about one.


#5

Got it. I cound’t find a easy way to get it done.