Knockout.js: Binding Multiple View Models in a View

I came across a situation which needs to render two set of form controls in a view. Both the sets will have controls with the same name. I’m using knockout.js for view model. I tried binding these multiple view models separately using ko.applyBindings(VM1) and ko.applyBindings(VM2) in the same view. But one set of controls worked and the other did not. On searching the web, I found couple of solutions for this problem.

1. Binding View Model Using DOM Element ID:

One of the option is to declare two view models in the same view and then bind them using the ko.applyBindings and adding the DOM element id as the second parameter. For this, we need to wrap the forms (or set of controls) in separate DOM element like <div>. Set unique element id for each <div> enclosing the controls. Use the id as the second parameter for ko.applyBindings and bind the view models separately. Below is the sample. In the sample if you see the control name bookTitle and btnSubmit are in both the forms. Still, using separate view models and binding the view model using DOM-Element id aggregates both the control sets.

&lt;div style=&quot;width: 600px; overflow: hidden;&quot;&gt;
    &lt;div style=&quot;width: 300px; float: left;&quot; id=&quot;Form1&quot;&gt;
        Book Title:
        &lt;input data-bind=&quot;value: bookTitle&quot; /&gt;&lt;br /&gt;
        Book Price:
        &lt;input data-bind=&quot;value: bookPrice&quot; /&gt;&lt;br /&gt;
        &lt;button data-bind=&quot;click: btnSubmit&quot;&gt;Submit&lt;/button&gt;
    &lt;/div&gt;
    &lt;div style=&quot;display: table-cell;&quot; id=&quot;Form2&quot;&gt;
        Author Name:
        &lt;input data-bind=&quot;value: authorName&quot; /&gt;&lt;br /&gt;
        Book Title:
        &lt;input data-bind=&quot;value: bookTitle&quot; /&gt;&lt;br /&gt;
        &lt;button data-bind=&quot;click: btnSubmit&quot;&gt;Submit&lt;/button&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;script src=&quot;./knockout-2.2.0.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
    var viewModel_1 = {
        bookTitle: ko.observable(&quot;ASP.NET For Everyone&quot;),
        bookPrice: ko.observable(&quot;$34.00&quot;),
        btnSubmit: function () { alert(this.bookTitle() + &quot;: &quot; + this.bookPrice()); }
    };

    var viewModel_2 = {
        authorName: ko.observable(&quot;Dr. Someone&quot;),
        bookTitle: ko.observable(&quot;ASP.NET For Everyone&quot;),
        btnSubmit: function () { alert(this.authorName() + &quot;: &quot; + this.bookTitle()); }
    };

    ko.applyBindings(viewModel_1, document.getElementById(&quot;Form1&quot;));
    ko.applyBindings(viewModel_2, document.getElementById(&quot;Form2&quot;));
&lt;/script&gt;

Knockout-js-Multiple-View-Models

2. Using With-Binding to segregate the controls :

Another option is to use with-binding. In this method, instead of dom-element id, we have to bind the <div> tag enclosing the controls using with. The advantage of with-binding is, we can nest one or more sets of control inside another. Below is the sample. If you see the sample, the <div> is bound using with. Also, there is only one view model. In the view model the two sets of the controls are segregated based on with-binding.

&lt;div style=&quot;width: 600px; overflow: hidden;&quot;&gt;
    &lt;div style=&quot;width: 300px; float: left;&quot; data-bind=&quot;with: Book&quot;&gt;
        Book Title:
        &lt;input data-bind=&quot;value: bookTitle&quot; /&gt;&lt;br /&gt;
        Book Price:
        &lt;input data-bind=&quot;value: bookPrice&quot; /&gt;&lt;br /&gt;
        &lt;button data-bind=&quot;click: btnSubmit&quot;&gt;Submit&lt;/button&gt;
    &lt;/div&gt;
    &lt;div style=&quot;display: table-cell;&quot; data-bind=&quot;with: Author&quot;&gt;
        Author Name:
        &lt;input data-bind=&quot;value: authorName&quot; /&gt;&lt;br /&gt;
        Book Title:
        &lt;input data-bind=&quot;value: bookTitle&quot; /&gt;&lt;br /&gt;
        &lt;button data-bind=&quot;click: btnSubmit&quot;&gt;Submit&lt;/button&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;script src=&quot;./knockout-2.2.0.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
    var viewModel = {
        Book: {
            bookTitle: ko.observable(&quot;ASP.NET For Everyone&quot;),
            bookPrice: ko.observable(&quot;$34.00&quot;),
            btnSubmit: function () { alert(this.bookTitle() + &quot;: &quot; + this.bookPrice()); }
        },
        Author: {
            authorName: ko.observable(&quot;Dr. Someone&quot;),
            bookTitle: ko.observable(&quot;ASP.NET For Everyone&quot;),
            btnSubmit: function () { alert(this.authorName() + &quot;: &quot; + this.bookTitle()); }
        }
    };

    ko.applyBindings(viewModel);
&lt;/script&gt;

Reference: Knockoutjs.com.


Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.