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 8082057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:57:13+00:00 2026-06-05T16:57:13+00:00

I’m using a combination of Knockout.js + the Knockout mapping plugin + jQuery in

  • 0

I’m using a combination of Knockout.js + the Knockout mapping plugin + jQuery in a rich web client that consumes JSON from a RESTful API.

I need some guidance on how to deal with null values in my view model. Here’s my scenario and the problem I’m running into:

Most of the data members returned by the REST API are nullable. To account for this, I’m passing to the mapping plugin a sample of JSON with null values:

um.jsonMaps.campaign = {
    "priority": null,
    "recipientListId": null,
    "autoPrepare": null,
    "timeToSend": null
}

I do the initial binding like this:

this.model = ko.mapping.fromJS(um.jsonMaps.campaign);

Here’s some data from an API call:

var data= {
    "priority": 95,
    "recipientListId": "a2aac72a-59f6-45da-a636-a48cc2b20137",
    "autoPrepare": false,
    "timeToSend": null
}

…which is bound like this:

ko.mapping.fromJS(data, this.viewModel.model);

The problem is that if users modify or touch any of the UI elements bound to this data, they implicitly turn the data member in the model into a quoted string literal. So, the integer 95 becomes “95” if the user adds some text and deletes it. And if a value that was null from the API is touched in the UI, it becomes “” (e.g. the empty string).

I need ints and nulls to remain as ints and nulls after editing.

  • 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-05T16:57:14+00:00Added an answer on June 5, 2026 at 4:57 pm

    In my project I ended up with creating view model over data from the server. I added new property with getter and setter so that it writes numeric value when required:

    function DataViewModel(dataModel) {
        var self = this;
        self.priority = ko.observable(dataModel.priority);
        self.recipientListId = ko.observable(dataModel.recipientListId);
        self.autoPrepare = ko.observable(dataModel.autoPrepare);
        self.timeToSend = ko.observable(dataModel.timeToSend);
    
    
    
        self.priorityKo = ko.computed({
            read: function () {
                return self.priority().toFixed(2);
            },
            write: function (value) {
                value = parseFloat(value.replace(/[^\.\d]/g, ""));
                if (isNaN(value)) {
                    self.priority(-1); // to fire 'changed' event
                    self.priority(0); // final value in case of incorrect user input
                } else {
                    self.priority(value); // real numeric value
                }
            },
            owner: this
        });
    

    your mapping will look like:

    this.model = ko.mapping.fromJS(new DataViewModel(um.jsonMaps.campaign));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.