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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:15:48+00:00 2026-06-04T20:15:48+00:00

I use the mapping plugin with my own prototype types From JSON var mapping

  • 0

I use the mapping plugin with my own prototype types

From JSON

var mapping = {
    Children: {
        create: function(options) {
            return ko.mapping.fromJS(options.data, {}, new ChildViewModel());
        }
    }
};

$.getJSON(url, null, function (data) {
    var model = ko.mapping.fromJS(data, mapping, new RootViewModel());
    ko.applyBindings(model);
});

To JSON

var data = ko.mapping.toJSON(this);

The json data has the orignal child items and the newly added items are just empty json objects {}

I add child items like this from the root model

addChild: function () {
    this.Children.push(new ChildViewModel());
}

Whats wrong?

Update:
A fiddle that recreates my problem

http://jsfiddle.net/pDnnM/

Update2
Tested another approach after some google findings still no dice :/

http://jsfiddle.net/5UDhB/

Update after answer

I have data both on main model and child models that need to be mapped and then serilized to JSON

http://jsfiddle.net/699YH/

So i need it to map both main model and children, maybe this is not possible?

Update CRUD operations how?
I’ve gotten further thanks to google and Madcapnmckay here at Stackoverflow. I have one final problen, and after this I think I can do everything with the mapping plugin that we currently are doing with our manual mappers (We are soon hitting over 100 view models so manual mappers are starting to frustrate us).

I havent found a way for the mapper to handle CRUD scenarios, lets say you have 5 items when you load the view, these have id’s, then you add 2 items, these get id: 0. Then we save send the entire VM to backend these new rows get new Id’s. I call the mapper plugin on the update but it fails with When calling ko.update*, the key '0' was not unique!

http://jsfiddle.net/3bWcF/2/

You can offcourse do

this.items.remove(function(item) { return item.id() == 0;});

Just before calling the mapper, But can the mapper do it for you? Thanks!

  • 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-04T20:15:49+00:00Added an answer on June 4, 2026 at 8:15 pm

    The issue is that your dynamically added children are not created by the mapping plugin. You can solve this and clean up your code by moving the mapping inside your objects like so.

    http://jsfiddle.net/madcapnmckay/3bWcF/1/

    var mappings = {
        items: {
            key: function(data) {
                return data.id;
            },
            create: function(options) {
                return new ChildViewModel(options.data)
            }
        }
    };    
    
    var counter = 5;
    
    ViewModel = function(config) {
        ko.mapping.fromJS(config, mappings, this);   
    
        this.json = ko.computed(function() {
            return ko.mapping.toJSON(this, { ignore: [ "json" ] });
        }, this);
    };
    
    ViewModel.prototype = {
        add: function() {
            this.items.push(new ChildViewModel({ 
                id: counter++, 
                name: "new row" + counter 
            }));
        }
    };
    
    ChildViewModel = function(config) {
        ko.mapping.fromJS(config, {}, this);
    };
    
    ko.applyBindings(new ViewModel(data));
    

    Hope this helps.

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

Sidebar

Related Questions

I use plugin Buffet , and there's local-to-buffer mapping d to delete buffer under
I am new to nHibernate. I understand how to use mapping using Fluent nHibernate.
In order to use normal mapping in GLSL shaders, you need to know the
I try to use this mapping : @Entity @Table(name=ecc.\RATE\) @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name=DISCRIMINATOR, discriminatorType= DiscriminatorType.STRING) public
Having this Entity for example: <?php namespace Gitek\HotelBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** *
I have this entity (Registro): <?php namespace Gitek\RegistroBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** *
I am trying to use the stored procedure mapping feature in Entity Framework to
I'm trying to use empty() in array mapping in php. I'm getting errors that
I need to use ClassMaps instead of auto mapping because of legacy database. But
I've decided to use Entity Framework for O/R Mapping, and DataAnnotations for validation in

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.