Reputation: 62484
Let's assume we have few ViewModels with the same property names like Id
, Name
, also
we have defined a View template (basically HTML) and want to use/bind a data from both ViewModels.
Question is how to specify a binding data context so it would be a way to indicate from which View Model to use bound properties?
Upvotes: 1
Views: 1158
Reputation: 1211
As noted on http://knockoutjs.com/documentation/observables.html, optionally you can pass a second parameter to ko.applyBindings
to define which part of the document you want to search for data-bind attributes. For example, ko.applyBindings(myViewModel, document.getElementById('someElementId'))
. This restricts the activation to the element with ID someElementId and its descendants, which is useful if you want to have multiple view models and associate each with a different region of the page.
Another option would be to use the with:
binding introduced on ko 1.3+, which renders the DOM based on a specific viewModel property. This is nice because if the property is null, nothing is rendered at all. Steve shared a live example about this feature on http://jsfiddle.net/StevenSanderson/f5w6u/3/light/
Upvotes: 2