Knockout applyBindings 忽略选择器
我真的很喜欢 ko 并且有一个关于 applyBindings 函数的问题。
看来,我可能做错了,ko 不喜欢将绑定应用到页面的同一部分。为了避免这种情况,我们可以传递上下文/元素作为第二个参数来告诉 ko 要使用页面的哪个区域。
我想要做的是传递第三个参数,告诉 ko 要忽略哪些元素/类。
本质上,我想要拥有使用不同模型的嵌套区域。
有谁知道这是否可行,或者其他人已经实现了?
谢谢
I'm really getting into ko and have a question regarding the applyBindings function.
It seems that, I may be doing it wrong, ko doesn't like to apply bindings to the same part of the page. To avoid this we may pass a context/element as a second parameter to tell ko which area of the page to use.
What I want to do is pass in a third parameter that tells ko which elements/classes to ignore.
Essentially, I want to have nested areas that use different models.
Does anyone know if this is possible, or anyone else has already implemented it?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于这个问题的一些想法:
1- 通常,您不想多次调用 ko.applyBindings 。正常的技术是使用
template
绑定或if
和with
绑定来确保某些区域在准备就绪之前不会被绑定。您甚至可以拥有一个仅包含页面每个区域的可观察值的整体视图模型。然后,您可以使用with
绑定来确保它们不会绑定得太快并交换它们。这是一个简单的、人为的示例,您可以在其中控制放入每个部分的模板名称和数据,并可以将它们交换出来: http://jsfiddle.net/rniemeyer/6f5E9/。有很多方法可以做到这一点,示例只是最简单的方法。
2- Knockout 确实允许自定义绑定 提供程序。您可以编写一个绑定提供程序,它会根据可配置的选择器简单地忽略某些元素。
类似于:
这个简单的元素不会处理应排除的元素的子元素,但可以对其进行增强以以某种方式标记子元素。
这是一个示例:
http://jsfiddle.net/rniemeyer/pC5rg/
A couple of thoughts on this one:
1- Typically, you would not want to call
ko.applyBindings
more than once. A normal techinique is to use thetemplate
binding orif
andwith
bindings to ensure that certain areas are not bound until something is ready. You can even have an overall view model that just contains observables for each area of your page. Then, you can usewith
bindings make sure that they don't bind too soon and to swap them out.Here is a simple, contrived example where you control the template name and data that you put in each section and can swap them out: http://jsfiddle.net/rniemeyer/6f5E9/. There are many ways that you could do this with the sample just being the most barebones approach.
2- Knockout does allow for custom binding providers. You could write a binding provider that would simply ignore certain elements based on a configurable selector.
Something like:
This simple one wouldn't handle children of elements that should be excluded, but it could be enhanced to mark the children in some way.
Here is a sample:
http://jsfiddle.net/rniemeyer/pC5rg/