KnockOutJS - 单个视图中的多个ViewModels [英] KnockOutJS - Multiple ViewModels in a single View
问题描述
所以我想知道它有多难将创建多个ViewModel并将它们全部加载到单个视图中。有一个注意事项,我还需要能够将
例如我有一个< select>
下拉菜单,那个选择下拉菜单有选择的状态,允许我将< select>
中的所选项目的ID传递到单独的ViewModel中的另一个Ajax调用....
在单个视图中处理大量ViewModels的任何点都赞赏:)
如果他们都需要在同一页面上,一个简单的方法是拥有包含其他视图模型的数组(或属性列表)的主视图模型。
masterVM = {
vmA:new VmA(),
vmB:new VmB(),
vmC:new VmC(),
}
然后,您的 masterVM
可以有其他属性如果需要,页面本身。在这种情况下,视图模型之间的通信并不困难,因为您可以通过 masterVM
继续,也可以使用 $ parent
/ $ root
在绑定或其他一些自定义选项。
I'm thinking that my application is getting quite large now, too large to handle each View with a single 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.
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....
Any points on dealing with numerous ViewModels in a single View appreciated :)
If they all need to be on the same page, one easy way to do this is to have a master view model containing an array (or property list) of the other view models.
masterVM = {
vmA : new VmA(),
vmB : new VmB(),
vmC : new VmC(),
}
Then your masterVM
can have other properties if needed, for the page itself. Communication between the view models would not be difficult in this situation as you could relay through the masterVM
, or you could use the $parent
/ $root
in bindings, or some other custom options.
这篇关于KnockOutJS - 单个视图中的多个ViewModels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!