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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:17:28+00:00 2026-06-04T14:17:28+00:00

I am trying to solve a small problem with ko mapping. The scenario is

  • 0

I am trying to solve a small problem with ko mapping. The scenario is that my ViewModel is basically a collection of objects. Each of the objects is created from a json call, in this way:

var ViewModel = ko.observableArray();

$.getJSON(url, function(data) {
    ViewModel.push(ko.mapping.fromJSON(data));
});

This works perfectly and I can do all sort of magic in my HTML.
The question is if for example I want to add something to my collection, let’s say to support client-side “Add and Edit” scenario. I would like to do something like:

<input type="button" value="add new" data-bind="click: AddNew" />

And I would like the AddNew function in the ViewModel to be something like:

function AddNew() {
    this.push(// WHAT HERE?);
}

Basically I need to push an object which is identical to the other already existing, but of course with all blanked out properties…

I was thinking of a way of “cloning” an object form the list and setting all the observables to empty but I would not know where to start I’m afraid :/

  • 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-04T14:17:29+00:00Added an answer on June 4, 2026 at 2:17 pm

    If you are going to expand your functionality to client-side edit/add then i would recommend formalizing your objects into a js class and then mapping these objects internally. This would allow you to have your add methods on your main viewmodel and create blank instances client side easily.

    One caveat with the mapping plugin is that for updating objects it expects those objects to have been originally mapped by the plugin. Here’s a quick example of how it could be done.

    var YourObjectClass = function (config) {
        var self = this, data;
    
        // your default structure goes here
        data = $.extend({
            name: "",
            id : -1
        }, config);
    
        ko.mapping.fromJS(data, {}, self);
    };
    
    var viewModel = function(initialData) {
        var self = this;
    
        ko.mapping.fromJS(initialData, {
            items: {
                create : function (options) {
                    return new YourObjectClass(options.data);
                }
            }
        }, self);
    
        this.AddNew = function () {
           self.items.push(new YourObjectClass());
        }
    };
    

    Hope this helps.

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

Sidebar

Related Questions

Below is a small test case that demonstrates a problem that I am trying
I have a small math problem I am trying to solve Given a number
Recently I was trying to solve a small AI problem but got stuck in
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
Trying to solve this problem . I would like to learn how the bootstrapper
I'm trying to solve the problem here but I don't know why my code
I am trying to solve this problem. I have a series of SELECT statements
I'm trying to solve the Adding Reversed Numbers problem (ADDREV) at the Sphere Online
edit: I was trying to solve a spoj problem. Here is the link to
I am trying to to solve a problem where I need to pass large

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.