从敲除应用绑定中排除DOM元素?

Alw*_*wyn 6 javascript asp.net-mvc dom asp.net-mvc-4 knockout.js

我想将我的淘汰视图模型定位到dom的某个部分,如下所示:

ko.applyBindings(MyViewModel,$('#Target')[0]);
Run Code Online (Sandbox Code Playgroud)

但是我不希望它适用于它下面的所有doms.这样做的原因是整个SPA的工作效果不佳 - 无法跟上由于将每个潜在的交互包含在一个巨大的对象中而产生的巨型视图模型.因此,页面由多个部分视图组成.我希望每个partials实例化自己的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>
Run Code Online (Sandbox Code Playgroud)

我又怎么能排除股利低于整个DOM树标有DoNotBindBelowThisapplyBindings

RP *_*yer 13

看看这里的博客文章:http://www.knockmeout.net/2012/05/quick-tip-skip-binding.html

基本上,您可以创建自定义绑定,如:

ko.bindingHandlers.DoNotBindBelowThis = {
    init: function() {
        return { controlsDescendantBindings: true };
    }
};
Run Code Online (Sandbox Code Playgroud)