Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:37:22+00:00 2026-06-14T20:37:22+00:00

I am using Stripe and to avoid any issues with PCI compliance the credit

  • 0

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:

  1. 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 a read is called on a computed observable)

  2. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:37:23+00:00Added an answer on June 14, 2026 at 8:37 pm

    Rather than doing all that, just define a toJSON function which will return an object that contains the properties you want serialized. i.e., create a copy of your view model and remove the properties from the copy you don’t want serialized.

    function ViewModel(model) {
        this.name = ko.observable(model.name);
        this.password = ko.observable(model.password);
    
        this.toJSON = function () {
            var copy = ko.toJS(this);
            delete copy.password;
            return copy;
        }
    }
    

    If you really wanted to do this, I suppose you could do this. The ko.toJS function goes through all the properties of an object and maps the value to a new object. So if it is not an observable, it will simply copy it, otherwise it will invoke (read) it. You need to trick knockout into unconditionally invoke your observable so you may always throw an error upon reading it.

    You can write up a new observable hierarchy that would be a non-caching version, but that’s a lot of stuff you probably don’t need. You just need to create an observable which throws errors ultimately. So what you can do is create a function that will just throw an error and trick knockout into thinking it is an observable. Fortunately, knockout checks this by seeing it has an observable as a prototype through a special property.

    ko.errorObservable = function (thrower) {
        function errorObservable() {
            if (typeof thrower === 'function') {
                thrower();
            }
        }
        // trick knockout into thinking this is an observable so it will be invoked
        errorObservable[ko.observable.protoProperty] = ko.observable;
        return errorObservable;
    };
    

    Then it’s just a matter of adding the observable to your view model and you’re set.

    function ViewModel(model) {
        this.name = ko.observable(model.name);
        this.password = ko.observable(model.password);
        this._neverSerialize = ko.errorObservable(function () {
            throw new Error("Serialization of this model is prohibited.");
        });
    }
    

    Of course, this relies on undocumented behavior. The way knockout determines these things may change in the future. Use at your own risk.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to zebra-stripe a html table without using any js stuff or writing
I am using Stripe payment system to accept credit cards with jQuery validator for
I need to strip any non-alphanumeric characters from the end of strings using PHP's
Is there any way to strip elements out of a web.xml file using ANT?
I'm using Stripe in my Rails 3 application to do creditcard payments. I'm roughly
I'm using Stripe to accept payments in my Rails 3 app. Stripe requires this
I'm testing a simple checkout form using stripe for payments. I followed tutorial examples
I'm using jquery to zebra stripe a table, and it's working fine - except
I'm using the stripes framework to realize a web application. In order to avoid
Using the Stripe API , I'd like to be able to query for date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.