javascript - Knockout Mapping overwriting Computed Observables, is this intentional? -


say, have setup below:

var model = function() {     this.base = ko.observable("initial");     this.dependent = ko.computed(function() {         return this.base() + ", computed";     }, this); };  var vm = new model(); 

at point, vm object { base: observable(), dependent: dependentobservable() } (as expected)

but, once run:

ko.mapping.fromjs({base: "hello", dependent: "hi"}, {}, vm) 

vm becomes object { base: observable(), dependent: "hi", __ko_mapping__: object }

dependent no longer observable, expecting untouched.

is intentional? if so, rationale behind this? shouldn't dependent observables kept out of mapping?


the reason question that, using ko.mapping.tojs() convert vm javascript object (which gives dependent property, expected).

then, sometime later, i'm using ko.mapping.fromjs() shown above. combination overwrites vm's ko.computed() property.


solutions come with:

  • explicitly specify dependent ignored during mapping.

    ko.mapping.fromjs({base: "hello", dependent: "hi"}, {'ignore': "dependent"]}, vm) 
  • make dependent writable (just namesake)

    this.dependent = ko.computed({     read: function() {         return this.base() + ", computed";     },     write: function() {},     owner:  }); 

misc. info

  • the line overwrites dependentobservable() (as far can understand)


Comments