Skip to content Skip to sidebar Skip to footer

Tojson View Model Overrides Not Being Reflected After Knockout Unmapping

I have a deep object graph from the server which I am representing with a Knockout view model structure which omits various unnecessary properties at each level but preserves the b

Solution 1:

A couple of things are going on here:

  • your toJSON function is getting called, but not as part of the ko.mapping.toJSON call. The mapping plugin does a stringify as part of determining a key for objects.
  • ko.mapping.toJSON creates a plan copy of the object and at that point it is no longer an instance of ParentViewModel. Then, when it stringifies the object, it will have no reason to run your toJSON function.

The call to ko.mapping.toJSON does support a second argument for options. You could pass { ignore: ['child'] } in as this argument like: http://jsfiddle.net/rniemeyer/4kHC2/

Post a Comment for "Tojson View Model Overrides Not Being Reflected After Knockout Unmapping"