Knockout:如何检查绑定是否已应用于页面区域

发布于 2024-12-27 10:50:31 字数 109 浏览 0 评论 0原文

有没有办法检查页面区域是否已调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救星 2025-01-03 10:50:31

Knockout 2.0 有两个函数可以用于此目的。 ko.dataForko.contextFor 接受一个节点并返回可用于在该范围 (dataFor) 进行绑定的 Knockout 数据或整个绑定上下文在该范围 (contextFor),其中包括 $data$parent$parents$root

因此,您可以执行以下操作:

var isBound = function(id) {
  return !!ko.dataFor(document.getElementById(id));
};

这是一个示例: 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 and ko.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:

var isBound = function(id) {
  return !!ko.dataFor(document.getElementById(id));
};

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 when mySubModel gets populated then that area would appear and be bound.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文