KnockOutJS - 单个视图中的多个 ViewModel [英] KnockOutJS - Multiple ViewModels in a single View

查看:22
本文介绍了KnockOutJS - 单个视图中的多个 ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的应用程序现在变得非常大,太大而无法使用单个 ViewModel 处理每个视图.

I'm thinking that my application is getting quite large now, too large to handle each View with a single ViewModel.

所以我想知道创建多个 ViewModel 并将它们全部加载到单个 View 中会有多困难.需要注意的是,我还需要能够将 X ViewModel 数据传递到 Y ViewModel 数据中,因此各个 ViewModel 需要能够相互通信或至少是互相了解.

So I'm wondering how difficult it would be to create multiple ViewModels and load them all into a single View. With a note that I also need to be able to pass X ViewModel data into Y ViewModel data so the individual ViewModels need to be able to communicate with each other or at least be aware of each other.

例如,我有一个 中传递所选项目的 ID. 到一个单独的 ViewModel 中的另一个 Ajax 调用......

For instance I have a <select> drop down, that select drop down has a selected state which allows me to pass the ID of the selected item in the <select> to another Ajax call in a separate ViewModel....

关于在单个视图中处理多个 ViewModel 的任何观点表示赞赏:)

Any points on dealing with numerous ViewModels in a single View appreciated :)

推荐答案

Knockout 现在支持多个模型绑定.ko.applyBindings() 方法接受一个可选参数——绑定将被激活的元素及其后代.

Knockout now supports multiple model binding. The ko.applyBindings() method takes an optional parameter - the element and its descendants to which the binding will be activated.

例如:

ko.applyBindings(myViewModel, document.getElementById('someElementId'))

这将激活限制为 ID 为 someElementId 的元素及其后代.

This restricts the activation to the element with ID someElementId and its descendants.

有关详细信息,请参阅文档.

See documentation for more details.

这篇关于KnockOutJS - 单个视图中的多个 ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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