Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Oh, that's easy. <a href="http://knockoutjs.com/documentation/observables.html" rel="nofollow"><code>applyBindings</code></a> method in Knockout JS accepts an optional DOM element as a second parameter. So in your case you can do something like:</p> <pre><code>ko.applyBindings(myViewModel, document.getElementById('myModuleWrapperDiv')); ko.applyBindings(otherViewModel, document.getElementById('otherModuleWrapperDiv')); </code></pre> <p>The only catch is that you won't be able to mix and match bindings from two view models in a single DOM sub-tree. For example this markup would work:</p> <pre><code>&lt;div id="myModuleWrapperDiv"&gt; ... &lt;/div&gt; &lt;div id="otherModuleWrapperDiv"&gt; ... &lt;/div&gt; </code></pre> <p>This, however, wouldn't:</p> <pre><code>&lt;div id="myModuleWrapperDiv"&gt; ... &lt;div id="otherModuleWrapperDiv"&gt; ... &lt;/div&gt; &lt;/div&gt; </code></pre> <hr> <p>Here's how I do widgets with Knockout. Please note, that in 95% of the cases it's totally fine to simply load a template and apply bindings to it. However, if you need a <em>packaged</em> widget (i.e. something that you would release as a separate component) the best way to go is to wrap it in a custom binding. Angular JS promotes a similar approach with its directives.</p> <ol> <li><p>Design markup for your widget as a document fragment. You can do it whatever you want: simple bit of HTML, a placeholder <code>div</code> with all rendering and interaction done in JavaScript - everything will do.</p></li> <li><p>Design a view model for your template. Think of the data that might be required for your widget to render and operate and build your model according to that.</p></li> <li><p>Create a custom binding for your widget. The simplest thing would be having it on a placeholder <code>div</code> element.</p> <p>a. Your <code>init</code> method should load the template and apply bindings of your internal widget view model to it. After bindings got applied you can append the resulting document fragment to a placeholder <code>div</code>.</p> <p>b. Your update method should change the state of your inner view model based on the values provided via custom binding attribute parameters.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload