Knockout:如何检查绑定是否已应用于页面区域
有没有办法检查页面区域是否已调用 applyBindings
?
看起来淘汰赛不喜欢在同一页面上多次调用 applyBindings
。
Is there a way to check whether applyBindings
has been called for a page area or not?
It seems knockout doesn't like when applyBindings
is called on the same page more than once.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Knockout 2.0 有两个函数可以用于此目的。
ko.dataFor
和ko.contextFor
接受一个节点并返回可用于在该范围 (dataFor) 进行绑定的 Knockout 数据或整个绑定上下文在该范围 (contextFor),其中包括$data
、$parent
、$parents
和$root
。因此,您可以执行以下操作:
这是一个示例: http://jsfiddle.net/rniemeyer/GaqGY/< /a>
但是,在正常情况下,您确实应该在页面上调用一次
ko.applyBindings
。但这取决于您想要实现的目标。查看此答案,了解有关管理多个视图模型的方法的一些建议:多视图应用程序的knockoutjs 模式示例。通常,您会执行
,当填充
mySubModel
时,该区域将出现并被绑定。Knockout 2.0 has two functions that you could use for this purpose.
ko.dataFor
andko.contextFor
take in a node and return either the Knockout data that would be available to it for binding at that scope (dataFor) or the entire binding context at that scope (contextFor), which includes$data
,$parent
,$parents
, and$root
.So, you can do something like:
Here is a sample: http://jsfiddle.net/rniemeyer/GaqGY/
However, in a normal scenario you should really look to call
ko.applyBindings
a single time on your page. It depends on what you are trying to accomplish though. Take a look at this answer for some suggestions on ways to manage multiple view models: Example of knockoutjs pattern for multi-view applications.Typically, you would do a
<div data-bind="with: mySubModel">
and whenmySubModel
gets populated then that area would appear and be bound.