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

  • Home
  • SEARCH
  • 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 8798381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:05:03+00:00 2026-06-14T00:05:03+00:00

Right now, I can simply initialize a bunch of observables with defaults like… self.a

  • 0

Right now, I can simply initialize a bunch of observables with defaults like…

self.a = ko.observable(data.A || "");
self.b = ko.observable(data.B || "");
self.c = ko.observable(data.C || "");

But if I wanted to do something like

var ViewModel = ko.mapping.fromJS(data) 

And set the defaults, how do I do it?

Do I have to actually do something like

var ViewModel = function(data) {
   ko.mapping.fromJS(data, {
     c: {
       create: function(options) { return ko.observable(options.data.c || "") }
     };
   }, this)
};

Which as you can see is a ton overhead. I also wanted to add, that the one benefit that stood out at me with the mapping library is that this made it possible to have one less place to update when property names change. But if indeed you have to explicitly state the property name in order to specify the create callback, that benefit is defeated.

Am I wrong and there is an easy way to cover this?

  • 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-14T00:05:05+00:00Added an answer on June 14, 2026 at 12:05 am

    If you need to set all fields with same default values you can construct mapping options dynamically iterating all data‘s properties:

    var ViewModel = function(data) {
        var mappingOptions = {};
        for (var key in data) {
            mappingOptions[key] = {
                create: function(options) {
                    return ko.observable(options.data || "123");
                }
            };           
        }
    
        ko.mapping.fromJS(data, mappingOptions, this);
    };
    

    Here is working fiddle: http://jsfiddle.net/vyshniakov/tDAGU/

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

Sidebar

Related Questions

Right now I can run a PHP script from my Mac terminal like echoing
This has probably something to do with my transformations, but right now I can't
Im working right now on a program that can divide, add, ect, but, im
Right now I can use this URL to request a Google Static Maps image
I have a website and right now users can just open a directory and
I'm playing around with associative arrays right now and I can't seem to figure
I'm pretty much at wits end right about now and can't seem to figure
i'm extremely frustrated right now. I can't figure out why my container won't extend
I can't seem to see the forest for the trees right now (Looked at
A particular regular expression is bugging me right now. I simply want to replace

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.