knockout.js - knockout adding new properties to already existing view model -
there knockout model:
var custommapping = { update: function (options) { var result = ko.mapping.fromjs(options.data); var self = this; self.computetest = ko.computed(function () { return "test" + self.newobs(); }); return result; } }; var model = function() { var self = this; self.test = ko.observable(); self.init = function() { // create ajax call // in success mapping ko.mapping.fromjs(data, custommapping, self); //this mapping should update existing model - adding new observables , computed } ko.applybindings(self, $("#id")[0]); } var m = new model(); m.init();
so: 1) have knockout model. within functions defined , list of observables.
2) knocokut binding called before data came server.
3) model data loaded using m.init()
.
this should call custom mapping , create new observables (based on data server), computed variables. however, custom binding doesn't work. doesn't create new observables. believe should 'update' mapping, not understand how can extend inside existing model
given code there's perhaps shortcut available: move ko.applybindings
call init
method, right below mapping
call.
failing you'll need use placeholder / empty / fake ajax result wire "default" view model.
Comments
Post a Comment