這是一個實現: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