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?
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>.
KnockoutJS is a beautiful JavaScript framework that helps you to create responsive and data rich user interface without making the JavaScript code dirty. When you start learning KnockoutJS, you tend to make habit of creating single a View Model and binding it globally or to one particular DOM and play with it.
When you start learning KnockoutJS, you tend to make habit of creating single a View Model and binding it globally or to one particular DOM and play with it. Once you start diving deep, you will realize, single view model is not enough for developing enterprise applications.
The method : ko.applyBindings ( viewmode, root dom element) accepts two arguments. The second argument comes helpful when you have multiple VM's in your page. where view1 and view2 are the root dom element for that model. For a JqueryMobile-SPA this will be the page ids for corresponding model. Show activity on this post.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With