Im trying to figure out the proper way to setup Knockout and Requirejs with multiple view models.
I have multiple view pages, each with it own view model, like:
- page1.html - page1.js
- page2.html - page2.js
- many more
I have a main.js where I require these view models
require([
"page1",
"page2"
, function ( page1, page2 ) {
if( $("#page1").length ){
ko.applyBindings(new page1());
}
if( $("#page2").length ){
ko.applyBindings(new page2());
}
});
pagex.js
define(["jquery", "knockout"], function($, ko) {
return function viewModel() {
console.log("foo");
};
});
I have a constant problem with applying bindings multiple times so with jquery is the only way I have got it work but this cant be the recommended way?
I have also tried using if ko.applyBindings(new page1,(), document.getElementById('page1'))
but it gives the same problem since I require all view models from main..
Even if I move the applybindings to the view models the same problem arise.