2013-03-13 30 views
6

我想定位淘汰賽視圖模型DOM的某些部分是這樣的:從knockout應用綁定中排除DOM元素?

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

不過,我不希望它應用到它下面所有的延遲性肌肉痠痛。原因在於整個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> 

我又怎麼能排除股利低於整個DOM樹標有DoNotBindBelowThisapplyBindings

回答

11

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

基本上,你可以創建自定義樣結合:

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

我不太明白,難道我們不應該返回controlsDescendantBindings假的,而不是真的? – Alwyn 2013-03-13 20:46:10

+0

'controlsDescendantBindings:true'告訴KO我們(這個綁定)會處理綁定到我們的孩子。在上面的樣本綁定中,所有這些都完成了,所以孩子們一個人待着。然後,您可以返回並將'ko.applyBindings'調用到具有此綁定的元素內部的特定DOM元素。 – 2013-03-13 21:09:47

+0

是的,它做到了。謝謝。 – Alwyn 2013-03-13 22:17:20