Computed property being observed doesn't fire if changed twice in a row

StackOverflow https://stackoverflow.com/questions/9445676

  •  12-11-2019
  •  | 
  •  

문제

I have an Ember.Object that I'm updating with a property like below, but if I change primaryDemo twice in a row, it doesn't fire, yet if I change primaryDemo, then Rate, it does change. I'm puzzled as to why this is and how I can fix it.

dependantChanged: function() {
    console.log('Firing change');
    this.get('_update')(this);
}.observes('primaryDemo', 'Rate', 'Totals'),

UPDATE: So the first answer and fiddle got me thinking as to what the problem was, and it's due to changing a property on an object and not the object itself. I think ember does a hash check to see if there is a difference. In my case I'm already using underscorejs, so I just change the property, then use _.clone(demo) before doing the set. I'd rather not do that, so will wait to see if there is a more elegant solution before closing this.

도움이 되었습니까?

해결책

You don't need to set primaryDemo again. In the example that does nothing. You need to force tell Ember to notify your observer. See this fiddle...

var demo = { Imps: 1, Demo: { Id: 2 } }

var obj = Ember.Object.create({
    dependantChanged: function() {
        console.log('Firing change');
    }.observes('primaryDemo', 'Rate', 'Totals'),
});

obj.set('primaryDemo', demo);
demo.Imps = 2;
obj.set('primaryDemo', demo);

// Notify observers on obj#primaryDemo
Ember.notifyObservers(obj, 'primaryDemo');

다른 팁

Can you give more details? I created a simple JSFiddle http://jsfiddle.net/JjbXb/ from your description but changing the same property in a row, as you say, works.

Are you sure the value of primaryDemo is different in your 2 consecutive calls?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top