Aurelia:修改ANY属性时的通知 [英] Aurelia: notification when ANY property is modified
问题描述
您是否看到任何方法来知道何时通过绑定修改了任何模型的属性? 我需要通用的东西,因为它将应用于所有形式的应用程序.这意味着我不能仅对模型的每个属性都有一个"property" Changed()可观察的回调.我正在考虑重写绑定引擎创建的属性设置器的方法,以便它们可以调用单个已定义的回调,但是我觉得有更好的方法.
Do you see any way to know when ANY model’s property has been modified through a binding? I would need something generic because it would be applied to all the forms of the application. This means I cannot just have a 'property’Changed() observable callback for every properties of the models. I’m thinking along the ways of overriding the properties setters created by the binding engine so they can call a single defined callback but I feel like there could be a better way.
推荐答案
好,我最终只是使用绑定引擎来观察所有属性更改.这使我无需修改现有模型即可实现isDirty检查.
Ok, I ended up just using the binding engine to watch all properties changes. This allowed me to implement my isDirty checks without modifying the existing models...
所以最终代码如下:
Object.getOwnPropertyNames(obj).forEach(p => {
this.subscriptions.push(this.binding.propertyObserver(obj, p)
.subscribe(() => this.updateDirty()));
});
每次更改属性后都会调用
我的 updateDirty()方法,并且无需对模型进行任何更改.
my updateDirty() method is called after every property change and no change was necessary to the model.
如果有人能提出更好的解决方案,我仍然很感兴趣,但这暂时满足了我的需求.
If anyone can come up with a better solution, I'm still interested but this fits my needs for the time being.
这篇关于Aurelia:修改ANY属性时的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!