Playing around with knockout.js, I still don't really get the underlying logic. In the following example, the static javascript array boxList
does not have any reference to or by the viewModel, which is in fact totally empty. Still, the foreach binding has no difficulties to grasp the array. Why?
note: obviously, the binding only works when ko.applyBindings
has been called, with an empty viewModel as argument.
HTML:
<div data-bind="foreach : boxList">
<div><input type="checkbox" ><label data-bind="text: name"></label></div>
</div>
JS:
var viewModel = {
};
var boxList = [
{ name : 'n1'},
{ name : 'n2' },
{ name : 'n3'},
{ name : 'n4' },
];
ko.applyBindings(viewModel);
JSFiddle: http://jsfiddle.net/e85sLmk4/