I am trying to use the mapping plugin inside the constructor so I can easily add other functions to the constructor and have the observable items automatically created from the mapping plugin. Here is the code that I currently have but it is neither mapping the observable variables that I provide it or including the 'save' function in the end viewmodel. Is there a better way to do this?
function ViewModel(model){
var self = this;
self = ko.mapping.fromJS(model);
self.save = function() {
var data = ko.toJSON(self);
$.post("/Licensing/edit", data, function(returnedData) {
// This callback is executed if the post was successful
});
}
};
var vm = new ViewModel(model);
ko.applyBindings(vm);
Your code is not working because
ko.mapping.fromJS(model);returns a new object what you assign to yourselflocal variable what you extend with thesavefunction.However this new object is not returned from your constructor function so you will end with with an empty object because not the originally passed in
thisis modified.You can fix this with returning
selffrom your constructor:Or you can tell the mapping plugin to don't create a new object just use the currently created one with passing
selfas the third argument: