serialization - Knockoutjs - function inside viewmodel causing undesirable recursion -
in knockout view model have save() function sends jquery post request. inside post request call ko.tojs(this)
.
whenever call save function browser becomes unresponsive , tells me there's recursion. upon debugging (by using breakpoints), found when call tojs() appears degree of cloning of object, , in doing cloning calls save() function, in turn calls tojs()... , there's recursion.
why happen, , there way avoid without using tojson()?
[i have question regarding tojson, , explains why don't want use it.]
for sake of completeness, here view model.
function vmdictionary(dict) { if (dict === null || dict === undefined) { return; } var self = this; // directly-assigned variables self.concepts = new vmconcepts(dict.concepts); self.deleted = ko.observable(dict.deleted); self.description = ko.observable(dict.description); self.includeinsearch = ko.observable(true); self.id = ko.observable(dict.id); self.languages = ko.observablearray(dict.languages); self.lastupdate = new vmchangerecord(dict.lastupdate); self.name = ko.observable(dict.name); self.public = ko.observable(dict.public); self.templatename = function(observable, bindingcontext) { return "dictionary-template"; }; // computed variables self.publictext = ko.computed(function() { return sp.utils.localize(self.public ? "public" : "private"); }); // exposed functions self.save = function () { $.ajax({ data: ko.tojson(self), datatype: "json", type: "post", url: [...], statuscode: { 200: function (response) { console.log(response); } }, error: function (xmlhttprequest, textstatus, errorthrown) { console.log(xmlhttprequest); console.log(textstatus); console.log(errorthrown); } }); }; }
update: added entire view model (above).
you must doing wrong, works in little fiddle me
viewmodel = function() { this.somedata = ko.observable("test"); this.dto = ko.observable(); }; viewmodel.prototype = { save: function() { this.dto(ko.tojs(this)); } }; var viewmodel = new viewmodel(); ko.applybindings(viewmodel); viewmodel.save();
Comments
Post a Comment