Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout binding multiple view models in a page by id

Tags:

knockout.js

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?

like image 722
Akshat Jiwan Sharma Avatar asked Oct 26 '12 11:10

Akshat Jiwan Sharma


People also ask

How to bind two view models in one view in Ko?

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>.

What is KnockoutJS and how to use it?

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.

Is single view model enough for enterprise applications in KnockoutJS?

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.

How do I use Ko applybindings in jQuery Mobile?

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.


1 Answers

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

like image 87
RP Niemeyer Avatar answered Oct 18 '22 03:10

RP Niemeyer