Multiple View Models with Knockout

In a recent project, I was required to create a form for user input in a web page. Each section of the form is distinct and serves a unique purpose, so I chose to use a tabbed layout . The order in which the forms are completed was not a strict requirement, so some visual indication of which sections were completed was also a requirement. I decided to use Knockout js for the data binding to take advantage its easy data bindings as well as the ko.computed method to determine which sections are complete. While creating a design for this, it became apparent that each section (tab) should be its own view model. The problem was that there really isn’t any documentation on how to create multiple view models on a single page. There is an obscure reference deep in the Knockout js documentation that does not clearly explain how to do it. After a bit of googling, I could not find any clear instructions on how to solve the problem; only some single line answers. After getting small hints from several google search results and some trial and error, I was able to come up with a clean solution. The solution is actually quite simple, but I thought I would share it with since having a clear example would have saved me a lot of time. The first thing to do is to create your sections on the page with a unique id. ```html

First Section

First Name:

First name:

Second Section

Second Name:

Second name:

``` One thing you may notice about this example is that it is using the same variable name called “name” in each section. This is one of the benefits of using multiple view models since those variables are only accessible in the scope of the view model. Second, you will need to define your view models. There is nothing special about how these are defined for this particular solution. ```javascript function firstViewModel() { var self = this; self.name = ko.observable("Enter Name"); }; function secondViewModel() { var self = this; self.name = ko.observable("Enter Name"); }; ``` Lastly, you will need to apply the bindings for the view models and assign them to the previously defined sections. ```javascript ko.applyBindings(new firstViewModel, $("#firstSection")[0]); ko.applyBindings(new secondViewModel, $("#secondSection")[0]); ``` Take note of the second parameter passed to the applyBindings method as it does the magic of isolating the view model to a specific element on the page. I am not sure why this isn’t clearly documented anywhere, but hopefully this post proves useful to anyone that is looking to add support for multiple view models on a single page. A working demo can be found [here](http://www.yojimbocorp.com/demos/multipleviewmodels.html)