wma*_*but 4 javascript ember.js
在Ember.js中,是否有一种添加观察者的好方法,该观察者将观察子类实例上的所有更改Ember.Object?
即(咖啡)
Bat = Ember.Object.extend
name: null
age: null
hank = Bat.create
name: 'Hank'
age: 2
#Something like this
hank.addObserverToAll myClass, 'handleChange'
Run Code Online (Sandbox Code Playgroud)
这是一个实现:http://jsfiddle.net/Sly7/GMwCu/
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');
Run Code Online (Sandbox Code Playgroud)
如您所见,它只处理属性,而不是函数(我认为这就是你想要的).
您还可以意识到它仅适用于传递给create()方法的属性,而不适用于类定义中指定的属性(即使用extend).
| 归档时间: |
|
| 查看次数: |
1194 次 |
| 最近记录: |