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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:43:01+00:00 2026-06-10T00:43:01+00:00

js and the knockout mapping plugin my problem is this: i have model and

  • 0

js and the knockout mapping plugin

my problem is this:

i have model and i am adding observables to it during runtime like so:

viewModel[value] = new ko.observable(valueFromTextBox);

this works ok and the observables are binded to the screen and this part works fine
the problematic part is when i try to take that model and convert it to JSON like so:

var JSON = ko.mapping.toJS(page.model);

when i debug this i see that page.model has all the new observables. but my final JSON has only the ones who were in the model to begin with. not the ones added later in runtime.
what is the correct way to add observables during runtime?

thanks

EDIT:

I will describe my entire scenario and then post my solution as an answer.

I am using ASP.Net MVC, i created a view model then made an ajax call to receive my actual model form the server. then use:

ko.mapping.fromJS(model, page.model);

after that the binding occurs later when the user adds new fields i used:

viewModel[value] = new ko.observable(valueFromTextBox);

and in the end before sending it back to the server i used:

 var JSON = ko.mapping.toJS(page.model);

in which point the added fields were not present in the JSON.

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

    after debugging

    ko.mapping.fromJS(model, page.model);
    

    i realized the function reads the values in the dictionary

        __ko_mapping__.mappedProperties
    

    which is creted during the use of the toJS function

    var JSON = ko.mapping.toJS(page.model);
    

    anyway because my new added observables were not present in the __ko_mapping__.mappedProperties dictionary they
    were also not present in the JSON object returned to me using the ko.mapping.toJS function
    so what i did to make it work is this:

    viewModel[value] = ko.observable(valueFromTextBox);
    viewModel.__ko_mapping__.mappedProperties[value] = true;
    

    I add an observable to my model but update my mappedProperties dictionary as well
    and that’s it. now when calling ko.mapping.toJS my entire model is returned as a JSON object including my added observables.

    I realize this is probably not the most elegant way of doing it. but it sure does work great.

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

Sidebar

Related Questions

I'm using the knockout mapping plugin, previously I would have set a subscribe like
If I have a view model that looks like this when using the knockout
I have a Knockout Viewmodel that uses the mapping plugin. After mapping the JSON
I'm having this problem using the knockout.mapping plugin in conjunction with RequireJS. Basically, the
I'm having a problem with the knockout-mapping plugin with IE8. Our situation is that
I'm experimenting with knockout and the mapping plugin and wondering why this doesn't work.
Suppose I have knockout.js template like this: <script type=text/html id=mytemplate> <label for=inputId>Label for input</label>
I want to bind template using knockout mapping like this Question 1 Option1 Option2
I have a piece of code working in knockout.js like this: <div> ... some
I am new to Knockout Js..I have this issue on my page that when

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.