如何停止对子元素进行knockout.js 绑定评估
使用淘汰赛,当您调用 ko.applyBinding(viewModel, "divId") 时,它会通过您绑定到的元素 ("divId") 的子元素进行递归绑定。我想在子节点停止此评估。有办法做到这一点吗?
为什么...
我想将整个页面绑定到导航视图模型,这将处理基本布局和...微笑...导航。在各个页面上,我想将某些区域绑定到不同的视图模型,这些视图模型不是导航视图模型的属性。目前,如果我这样做,我会收到“无法解析绑定”错误,因为导航视图模型没有所需的属性。如果我可以停止沿着 dom 进行绑定,我就可以单独绑定这些项目。
Using knockout, when you call ko.applyBinding(viewModel, "divId")
it does a recursive binding down through the children of the element you bound to ("divId"). I would like to stop this evaluation at a child node. Is there a way to do this?
the reason why...
I would like to bind the entire page to a navigation view model, this will handle basic layout and ...smile... navigation. On the various pages I would like to bind certain regions to different view models that are not properties of the navigation view model. At the moment if I do this I get "unable to parse binding" errors as the navigation view model does not have the required properties. If I could stop the binding walking down the dom, I could just bind these items separately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过多种方式继续这一过程。通常,您可以将多个“子”视图模型添加到主视图模型中,然后在各个区域上使用
with
绑定与实际视图模型来绑定它们。从技术上来说,做你想做的事是可能的。您可以创建一个自定义绑定,告诉 KO 它将处理子级本身的绑定。它看起来像:
当你把它放在一个元素上时,KO 将忽略子元素。然后,您可以使用不同的视图模型对此元素的子元素调用 ko.applyBindings。
示例: http://jsfiddle.net/rniemeyer/tWJxh/
但通常情况下,您会使用多个视图使用
with
绑定在主视图模型下的模型。There are several ways that you can go on this one. Typically, you would add multiple "sub" view models to a main view model and then use the
with
binding on the various areas with the actual view models to bind against them.It is possible to technically do what you are after. You can create a custom binding that tells KO that it will handle binding the children itself. It would look like:
When you place this on an element, then KO will ignore the children. Then, you could call ko.applyBindings on a child of this element with a different view model.
Sample: http://jsfiddle.net/rniemeyer/tWJxh/
Typically though, you would use multiple view models underneath a main view model using the
with
binding.我这样做的一种方法是创建一个导航部分(或只是一个)并将 navVM 绑定到它。然后为内容创建另一个部分并将 contentVM 绑定到它。这样就不会有冲突,而且一切都非常分开。
然后执行 ko.applyBinding(navVM, "navSection") 和 ko.applyBinding(contentVM, "contentSection")
One way I have done this is to create a section for the navigation (or just a ) and bind the navVM to it. Then create another section for the content and bind the contentVM to it. That way there is no conflict and its all very separated.
Then do ko.applyBinding(navVM, "navSection") and ko.applyBinding(contentVM, "contentSection")