1

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?

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Great, with: is what I'm interested in, especially for nested ViewModels that's great thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.