Reputation: 5355
There is a 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 do mapping
ko.mapping.fromJS(data, customMapping, self); //this mapping should update existing model - adding new observables and computed
}
ko.applyBindings(self, $("#id")[0]);
}
var m = new model();
m.init();
So: 1) I have knockout model. Within it some functions are defined and also a list of observables.
2) Knocokut binding is called before data came from server.
3) Model data are loaded using m.init()
.
This should call custom mapping and create new observables (based on data from server), as well as some computed variables. However, this custom binding doesn't work. It doesn't create any new observables. I believe that this should be 'update' mapping, but I do not understand how can I extend inside it existing model
Upvotes: 0
Views: 391
Reputation: 63719
Given your code there's actually perhaps a shortcut available: move the ko.applyBindings
call to the init
method, right below the mapping
call.
Failing that you'll need to use a placeholder / empty / fake Ajax result to wire up a "default" view model.
Upvotes: 1