这是一个实现:3210
App = Ember.Application.create();
App.WatchedObject = Ember.Object.extend({
firstProp: null,
secondProp: "bar",
init: function(){
this._super();
var self = this;
Ember.keys(this).forEach(function(key){
if(Ember.typeOf(self.get(key)) !== 'function'){
self.addObserver(key, function(){
console.log(self.get(key));
});
}
});
}
});
App.watched = App.WatchedObject.create({
firstProp:"foo",
plop: function(){},
thirdProp: 'far'
});
App.watched.set('firstProp', 'trigObserver');
App.watched.set('secondProp', 'doesNotTrigObserver');
App.watched.set('thirdProp', 'alsoTrigObserver');
正如你所看到的,它只能处理性能,而不是功能(我认为这是你想要的)。
您也可以认识到,它只适用于传递给create()
方法的属性,而不适用于类定义中指定的属性(即使用extend
)。
这可能有所帮助:http://stackoverflow.com/questions/9193712/how-do-i-observe-all-property-changes-on-a-model-object – Rajat 2012-07-24 06:18:17
谢谢!但看起来那个可怜的家伙从来没有得到好的答复......而他在5个月前问道。 :( – wmarbut 2012-07-24 13:15:37
@Rajat也链接在那篇文章看起来不再有效或当前版本的烬 – wmarbut 2012-07-26 18:23:33