Akshat Jiwan Sharma
Akshat Jiwan Sharma

Reputation: 16000

Knockout binding multiple view models in a page by id

I am building a complex UI using twitter bootstrap and Knockout.js. To achieve this I have broken my page into a number of different logical components.

Is it okay to define a view model for each component and bind it by id , using

ko.applyBindings(myViewModel, document.getElementById('someElementId'))

rather then defining a single view model and binding the entire page to that?Does it have any performance issues?

Upvotes: 10

Views: 12221

Answers (1)

RP Niemeyer
RP Niemeyer

Reputation: 114792

It is fine to bind in this manner and it will not have worse performance.

When you are binding your subsections, you would want to make sure that you are not applying bindings to the same area more than once. This can happen if you apply bindings to a parent element and then to a child.

If you need to do that (bind individual sections, but also an overall view model), then you would want to do something like this: http://www.knockmeout.net/2012/05/quick-tip-skip-binding.html

Upvotes: 17

Related Questions