javascript - 从 knockout 应用绑定(bind)中排除 DOM 元素?

标签 javascript asp.net-mvc dom asp.net-mvc-4 knockout.js

我想将我的 knockout View 模型定位到 dom 的特定部分,如下所示:

ko.applyBindings(MyViewModel,$('#Target')[0]);

但是我不希望它应用于它下面的所有 dom。这样做的原因是整个 SPA 工作不是很好——无法跟上由于将每个潜在交互包含到一个巨大对象中而产生的巨型 View 模型。因此,该页面由多个部分 View 组成。我希望每个部分实例化自己的 ViewModel 并为父级提供交互界面。

一些样本dom

<div id="Target">
     <!--Everything here should be included except-->
     <div data-bind="DoNotBindBelowThis:true">
          <!--Everything here should NOT be included by the first binding, 
              I will specifically fill in the binding with targetted
              ApplyBind eg. ko.applyBindings(MyOtherViewModel, $('#MyOtherTarget')[0])
              to fill the gaps-->
            <div id="MyOtherTarget">
            </div>
     </div>
</div>

同样,如何从 applyBindings 中排除带有 DoNotBindBelowThis 标记的 div 下面的整个 dom 树?

最佳答案

查看此处的博文:http://www.knockmeout.net/2012/05/quick-tip-skip-binding.html

基本上,您可以像这样创建一个自定义绑定(bind):

ko.bindingHandlers.DoNotBindBelowThis = {
    init: function() {
        return { controlsDescendantBindings: true };
    }
};

关于javascript - 从 knockout 应用绑定(bind)中排除 DOM 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15395872/

相关文章:

javascript - 在 Firefox 中记录并稍后回放 DOM 更改?

javascript - Android网页中的链接变蓝

c# - ASP.NET 返回 View ("Index");返回同一标签

c# - MVC 脚手架不加载模型或上下文

javascript - typescript 3 : JSX element type 'Component' does not have any construct or call signatures. [2604]

javascript - Java Controller 是否可以将属性传递给成功的 ajax 调用?

asp.net-mvc - ASP.NET MVC 5(Visual Studio 2013 预览版)更改 [授权] 的登录 URL

javascript - Safari:绝对定位的 DIV 在通过 DOM 更新时不会移动

javascript - 如何判断 dom-repeat 何时完成冲压元素

javascript - Hook document.createElement 脚本来更改 src