title: Prevent an object from being serialized with Knockout.js toJS/toJSON
body: <p>I am using <a href="https://stripe.com/" rel="nofollow">Stripe</a> 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.</p>
  
  <p>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 <code>ko.toJS</code> or <code>ko.toJSON</code> or <code>ko.mapping.*</code> equivalents).</p>
  
  <p>To achieve this I thought I could do something clever, like this (in CoffeeScript):</p>
  
  <pre><code>class NeverSerialize
    datums = ko.observable()
    blocker = ko.computed(
      read: () -&gt; throw new Error("You shall not pass!")
      deferEvaluation: true
    )
  </code></pre>
  
  <p>and when <code>ko.toJS/toJSON</code> or equivalent is called it would execute <code>blocker()</code> and that would call the <code>read</code> function and be the end of it.</p>
  
  <p>This idea almost works - as you can see from <a href="http://jsfiddle.net/bmh_ca/xYkWA/" rel="nofollow">this jsFiddle</a>. Unfortunately it only executes <code>read()</code> the first time, but it is never called after. I gather this is because there are no observables that <code>read</code> depends upon, so knockout presumes that the value of the computed will not have changed.</p>
  
  <p>So I guess I am curious about two things:</p>
  
  <ol>
  <li><p>Is this conceptually a sensible way to go about preventing an observable from being serialized with <code>ko.toJS/toJSON</code>? (or, generically, having a function execute whenever a <code>read</code> is called on a computed observable)</p></li>
  <li><p>How would one go about having a computed variable's <code>read</code> function called every time it is accessed (i.e. avoid caching it)?</p></li>
  </ol>
  
  <p>I am just curious about how one might go about this sort of thing with Knockout.js.</p>