Ember.js属性观察者异常行为
我在Ember的一位房地产观察家那里遇到了一些不寻常的行为。请参阅此jfiddle以了解正在运行的代码:Ember.js属性观察者异常行为,ember.js,Ember.js,我在Ember的一位房地产观察家那里遇到了一些不寻常的行为。请参阅此jfiddle以了解正在运行的代码: 当观察者观察两个属性时,当两个属性同时更新时,它将被触发两次 当在控制器上为另一条路线设置观察者时,它将不会开始激发,直到该路线处于活动状态(预期)。但当离开该路线时,观察者将继续开火(不是故意的) 关于行为2的更多讨论:首次加载应用程序时,可以单击“更改多个属性操作”,属性将开始递增。但是,观察员没有被解雇。现在导航到观察者路线。当您增加属性时,观察者将触发。然后导航回索引路由。观察者将继
App = Ember.Application.create();
App.Router.map(function(){
this.resource('observer');
});
App.Router.reopen({location:'none'});
App.ApplicationController = Ember.Controller.extend({
consoleProp: function(){
console.log(this.get('controllers.application.prop1'));
},
changeTwoProperties: function(){
this.get('controllers.application').incrementProperty('prop1');
this.get('controllers.application').incrementProperty('prop2');
},
prop1:0,
prop2:0
});
App.IndexController = Ember.Controller.extend({
needs: ['application'],
changeTwoPropertiesBinding: 'controllers.application.changeTwoProperties',
consolePropBinding: 'controllers.application.consoleProp'
});
App.ObserverController = Ember.Controller.extend({
needs: ['application'],
changeTwoPropertiesBinding: 'controllers.application.changeTwoProperties',
consolePropBinding: 'controllers.application.consoleProp',
theObserver: function(){
console.log('observer fired');
}.observes('controllers.application.prop1', 'controllers.application.prop2')
});
对于第一部分,请使用
Ember.beginPropertyChanges()
和Ember.endPropertyChanges()
。看
对于第二种行为,需要更多的研究。我认为在您访问观察者路线之前不会创建
observer控制器
,但是当您离开路线时,它不会被破坏。Shimon:我也有类似的问题。我尝试了一切,包括与Ember.run
通信,但都没有用。如果hasChanged
方法只在所有3个控制器都完成更新后执行一次,那就太好了:@Wildhoney我认为您无法将3个(或多个)不同控制器上的更改作为单个更改进行观察。
Ember.beginPropertyChanges();
this.get('controllers.application').incrementProperty('prop1');
this.get('controllers.application').incrementProperty('prop2');
Ember.endPropertyChanges();