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

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

问题描述

假设我有一个带有 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) 来轻松实现,因为它将递归地解包所有 observable.您需要注意不要将自己包含在 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.

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

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