javascript - Knockout JS: Control Serialisation of Custom Object inside ViewModel -


i came across little gem of blog post outlining nice way control object serialisation in knockoutjs.

however, trying apply principle custom objects form properties on main viewmodel.

for example (parts lifted referenced link):

function person(first, last) {     this.first = ko.observable(first);     this.last = ko.observable(last);     this.full = ko.dependentobservable(function() {         return this.first() + " " + this.last();     }, this);      this.educationcollege = new ko.educationvariable();     this.educationschool = new ko.educationvariable(); }  person.prototype.tojson = function() {     var copy = ko.tojs(this);     delete copy.full;     return copy;  };  ko.educationvariable = function() {     return {         institution: ko.observable(),         grade: ko.observable()     }; };  ko.educationvariable.prototype.tojson = function() {     var copy = ko.tojs(this);     return copy.institution + ": " + copy.grade; }; 

as can see, serialisation of person controlled via prototype tojson override , works flawlessly.

however, note person has 2 properties of custom ko.educationvariable type.

i such properties serialised respective override in ko.educationvariable.prototype.tojson - not working.

since looks override-feature not work "nesting", there way control serialisation instances of specific object, other moving logic main viewmodel's tojson override?


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -