function viewModel_1(){
var vm = this;
// local observable
vm.title = ko.observable('This is Module 1');
// public observable (initializing with value)
// public values can only be initialized once, the
// first model to give them a value sticks
vm.val = ko.public('val', 'hello');
// public observable (not yet initialized with a value)
// but it will be filled in vm2 with an array
vm.list = ko.public('list');
// allows us to fill our list observable array with
// new values
vm.add = function(){
vm.list.push(vm.val());
vm.val('');
console.log(vm.list(), vm.list);
};
}
function viewModel_2(){
var vm = this;
// local observable
vm.title = ko.observable('This is Module 2');
// public observable (value initialized in vm1)
vm.val = ko.public('val');
// when a public is initialized with an array, the
// observable is converted to an observable array
vm.list = ko.public('list', ['hello', 'there']);
}
// because we are initializing the list from vm2, it's
// important that we instatiate our viewmodels prior to
// applying bindings. otherwise the bindings that are in
// vm1 will be applied against an observable (no array)
// and we'll get errors.
var vm1 = new viewModel_1();
var vm2 = new viewModel_2();
// apply the bindings
ko.applyBindings(vm1, document.getElementById('module1'));
ko.applyBindings(vm2, document.getElementById('module2'));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.