I've been using knockout.js for months now and only just realized it invokes functions when calling ko.toJS
when serializing my model.
This can lead to obvious problems like infinite loop, and at worst dangerous data corruption if a function with side effects is being called.
This behavior was at one point changed in an earlier version of Knockout, but it seems it was backtracked upon as being by design because sometimes some people want functions to be copied.
Well I never do (I don't think I do) and I'd really appreciate a way to be able to call toJS
that would not invoke my functions.
I realize I can use toJSON
instead but sometimes you really do want toJS
in the case where you want to do something like this:
toJSON = () => // this is typescript syntax
{
var copy = ko.toJS(this);
// remove credit card details if not selected payment type
if (this.paymentType() != 'CreditCard')
{
delete copy.creditCardDetails;
}
return copy;
}
Or perhaps I'm passing the model to some utility method that needs objects.
I guess I ultimately don't understand why the invocation of functions on my viewmodel isn't configurable and if there's an easy way to 'monkey patch' it or create a new toJS2
function then I'd really like to be able to do that.
There is some very useful information in this question, How can I use ko.toJs method without computed properties in knockout mapping? but as yet I've found no solution if I want to retain objects rather than convert to a string.