观察Ember Data的任何属性的更改 [英] Observe change on any attribute of an Ember Data
问题描述
用例是,我有一个 Decays
mixin,有一些CP表示自从更新模型以来已经有多长时间了。我希望这些CP能够更新,如果任何一个模型的attrs已经改变。 isDirty
也不会工作,因为我的用例使用 store.push
来更新模型。
我不认为有一种内置的方式来做,但这不是很难做到。只需为所有模型创建基类,并覆盖集合
函数。这样的东西:
var BaseModel = DS.Model.extend({
set:function(keyName,value) {
var ret = this._super.apply(this,arguments);
var attributes = Ember.get(this.constructor,'attributes');
if(attributes [keyName]){
this.trigger('attributeChanged');
}
return ret;
}
});
然后,您可以监听任何属性的更改:
model.on('attributeChanged',function(){});
我想你甚至可以在控制器或类似对象的情况下做这样的事情(虽然我没有测试它):
Controller.extend(Ember.Evented,{
modelPropertyChanged:function()
} .on('model.attributeChanged')
});
Is this possible? Something like .observes('_data')
, but not.
The use case is, I have a Decays
mixin that has some CPs that say how long its been since a model has been updated. I'd like these CPs to be able to update if any of the model's attrs have changed. isDirty
also won't work, since my use case uses store.push
to update the model.
I don't think there's a built-in way to do it, but it wouldn't be very difficult to make one. Just create a base class for all of your models and override the set
function. Something like this:
var BaseModel = DS.Model.extend({
set: function(keyName, value) {
var ret = this._super.apply(this, arguments);
var attributes = Ember.get(this.constructor, 'attributes');
if (attributes[keyName]) {
this.trigger('attributeChanged');
}
return ret;
}
});
Then, you can listen for changes to any attribute like this:
model.on('attributeChanged', function() {});
I think you can even do something like this in the case of a controller or similar object (although I haven't tested it):
Controller.extend(Ember.Evented, {
modelPropertyChanged: function() {
}.on('model.attributeChanged')
});
这篇关于观察Ember Data的任何属性的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!