javascript - Knockout JS Mapping fromJS nested models -


i having trouble understanding how work knockout js mapping plugin. have nested models (as seen below) , doing using ko.mapping.fromjs() in parent model. noticing computed values not being ...computed.

i tried understand "create":

var mapping = {     'children': {         create: function(options) {             return new mychildmodel(options.data);         }     } } var viewmodel = ko.mapping.fromjs(data, mapping); 

but right in current scenario not sure how implemented.

current structure:

var productmodel = function($name, $price, $quantity) {     var self = this;      self.id = ko.observable();     self.name = ko.observable($name);     self.quantity = ko.observable($quantity);     self.price = ko.observable($price);     self.price.total = ko.computed(function() {         return self.price() * self.quantity();     }); };   var cartmodel =  function (){    var self = this;     // model properties    self.id = ko.observable();       self.products = ko.observablearray();         self.fetch = function() {         $.ajax({             url: "route specific cart",             type: "get",             success: function(data) {                                 ko.mapping.fromjs(data, {}, self);             }         });     };         // convert mapping format     ko.mapping.fromjs(ko.mapping.tojs(self)); };  var viewmodel = ko.validatedobservable(new cartmodel()); ko.applybindings(new viewmodel()); 

i not sure how productmodel trigger computed inside productmodel, not sure if have call ko.mapping.fromjs inside every single model (i have many more models, stripped them out make simpler).

after fetch function alerted products().length , contains quantity saved. computed not showing. how implement create method of knockout mapping plugin nested observable(and observablearrays) models computed inside of them.

p.s: reason have self.price.total in order avoid tojs send total key well. can't ignore properties of nested models using ignore:

see thread more: knockout js mapping fromjs nested models

how ajax response looks like? returns whole cart, or products?

anyway, if want handle product list observablearray of viewmodels this:

ko.utils.arrayforeach(products, function(item, index) {   self.products.push(new productmodel(item.name, item.price, item.qty); }); 

if want use mapping plugin, haven't made custom implementation of "create" method, like:

var productmodel = function(product) {   var self = this;    ko.mapping.fromjs(product, {}, self);    self.total = ko.computed(function() {       return self.price() * self.quantity();   }); }; 

edit

i have re-read question, , think this answer may you're looking for, downside of approach, me @ least, need define mapping config object each viewmodel nested viewmodels in code


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -