knockout.js - which function will be consider as view model in knockout js -
i new in knockout js. please see below code , tell me function consider view model ?
there 2 function 1 cartline
, other 1 cart.............which function consider view model ?
see code ko.applybindings(new cart());
apply binding pointing cart function.......so mean cart()
consider view model ? if yes should cartline()
? child or nested view model ?
looking guidance. code taken jsfiddle http://jsfiddle.net/3bu6nybk/15/
var cartline = function () { var self = this; self.products = ko.observablearray(_products); self.product = ko.observable(1); self.price = ko.observable(1); self.quantity = ko.observable(1); self.product.subscribe(function(item){ if(!item) { self.price(0); self.quantity(0); return; } self.price(item.price); self.quantity(item.quantity); }); self.subtotal = ko.computed(function () { return self.price() * self.quantity(); },self); }; var cart = function () { // stores array of lines, , these, can work out grandtotal var self = this; self.lines = ko.observablearray([new cartline()]); // put 1 line in default self.formatcurrency = formatcurrency; };
if enclose of code in variable this:
var viewmodel = { //your code here }
then call apply bindings so:
ko.applybindings(new viewmodel());
everything in viewmodel.
see link more info: http://knockoutjs.com/documentation/observables.html
Comments
Post a Comment