Knockout.js:如何一次获得所有属性的通知 [英] knockoutjs: how to get notications for all the properties an once

查看:56
本文介绍了Knockout.js:如何一次获得所有属性的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含100个道具的ViewModel.当前,我需要一个处理程序,如果任何道具发生更改,则将调用该处理程序.当然,我可以为每个属性写入100 .subscribe,但似乎有更好的方法.就像在C#中一样,您可以在其中绑定到模型的PropertyChanged事件,然后通过它们的名称选择感兴趣的属性.

Suppose I have a ViewModel with 100 props. Currently I need a one handler that will be called if any of the props changes. Of course I can write 100 .subscribe for every property, but it seems, that there is a better way. Like in C#, where you can bind to PropertyChanged event of the model, and then choose properties of interest by their names.

推荐答案

一般的答案是创建一个可订阅所有内容的dependentObservable.通过在dependentObservable内部执行ko.toJS(viewModel)可以轻松完成此操作,因为它将以递归方式解开所有可观察对象.您将需要注意不要将自己包括在ko.toJS调用中,否则您将陷入无限循环.

The general answer is to create a dependentObservable that subscribes to everything. This can be easily accomplished by doing a ko.toJS(viewModel) inside of a dependentObservable, as it will recursively unwrap all observables. You will want to take caution to not include yourself in the ko.toJS call or you can get into an infinite loop.

如果您正在寻找具有更多功能的产品,请查看此

If you are looking for something with a little more functionality, then take a look at this post.

这篇关于Knockout.js:如何一次获得所有属性的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆