There are a few directions that you could go with this one.
One option is to call ko.applyBindings
with distinct view models against separate DOM elements like:
var viewModelA = { name: "Bob" };
var viewModelB = { price: 50 };
ko.applyBindings(viewModelA, document.getElementById("aContainer"));
ko.applyBindings(viewModelB, document.getElementById("bContainer"));
http://jsfiddle.net/9abgxn8k/
In this case, you would want to make sure that the elements are not ancestors of each other (don't want to bind anything twice)
Another option is to use sub view models:
var subModelA = { name: "Bob" };
var subModelB = { price: 50 };
var viewModel = {
subModelA: { name: "Bob" },
subModelB: { price: 50 }
};
ko.applyBindings(viewModel);
In this method, you would then use with
bindings on the areas that you want to display with each view model. You can control visibility with flags on the sub models or on the top model.
Another option that I like is to give your "views" a little bit of structure and do something like:
var View = function(title, templateName, data) {
this.title = title;
this.templateName = templateName;
this.data = data;
};
var subModelA = {
items: ko.observableArray([
{ id: 1, name: "one" },
{ id: 2, name: "two" },
{ id: 3, name: "three" }
])
};
var subModelB = {
firstName: ko.observable("Bob"),
lastName: ko.observable("Smith")
};
var viewModel = {
views: ko.observableArray([
new View("one", "oneTmpl", subModelA),
new View("two", "twoTmpl", subModelB)
]),
selectedView: ko.observable()
};
ko.applyBindings(viewModel);
http://jsfiddle.net/rniemeyer/PctJz/