I am using Stripe and to avoid any issues with PCI compliance the credit card data entered on my forms should never be transmitted from browser to server. Users shall enter credit-card data into forms with Knockout.js.
Mostly as a matter of interest (mostly so I understand better how knockout.js works), I would like to come up with a way to prevent serialization of credit-card data by having an explicit error occur whenever serialization of the data is attempted in the usual knockout.js way (ie ko.toJS
or ko.toJSON
or ko.mapping.*
equivalents).
To achieve this I thought I could do something clever, like this (in CoffeeScript):
class NeverSerialize
datums = ko.observable()
blocker = ko.computed(
read: () -> throw new Error("You shall not pass!")
deferEvaluation: true
)
and when ko.toJS/toJSON
or equivalent is called it would execute blocker()
and that would call the read
function and be the end of it.
This idea almost works - as you can see from this jsFiddle. Unfortunately it only executes read()
the first time, but it is never called after. I gather this is because there are no observables that read
depends upon, so knockout presumes that the value of the computed will not have changed.
So I guess I am curious about two things:
Is this conceptually a sensible way to go about preventing an observable from being serialized with
ko.toJS/toJSON
? (or, generically, having a function execute whenever aread
is called on a computed observable)How would one go about having a computed variable's
read
function called every time it is accessed (i.e. avoid caching it)?
I am just curious about how one might go about this sort of thing with Knockout.js.