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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:03:47+00:00 2026-06-17T09:03:47+00:00

The problem As I was working and exploring knockoutjs, I got stuck at some

  • 0

The problem

As I was working and exploring knockoutjs, I got stuck at some point. I want to serialize a viewmodel (and underlying viewmodels) to JSON. This will end up in an infinite loop because the child viewmodels have a property which references the parent viewmodel. What is the best practice for solving this issue?

The code

var Partner = function (parent) {
    var self = this;
    self.parent = parent;
    self.name = ko.observable('');
}

var ProjectViewModel = function () {
    var self = this;
    self.nr = ko.observable(0);
    self.tite = ko.observable('');
    self.partners = ko.observableArray();            

    self.addPartner = function () { self.partners.push(new Partner(self)) };
    self.removePartner = function (c) { self.partners.remove(c) };
};
var vm = new ProjectViewModel();
ko.applyBindings(vm);


$("#button").click(function () {
    alert(ko.toJSON(vm));
}

What I tried so far

I tried adding the following method in the Partner viewmodel:

Partner.prototype.toJSON = function () {
            var copy = ko.toJS(self);
            delete copy.parent;
            return copy;
        }

This works with only one Partner, if the ProjectViewModel has multipe partners, every partner will have the same value as the last partner. This happens only when I want to serialize it to 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-17T09:03:48+00:00Added an answer on June 17, 2026 at 9:03 am

    There are several ways to handle this situation. The ko.toJS part of KO handles this properly. It is ultimately JSON.stringify (called after ko.toJS in ko.toJSON that causes the error.

    Your toJSON method on your prototype is pretty close, except that you want to be dealing with this rather than self.

    So, it would look like:

    Partner.prototype.toJSON = function() {
      var copy = ko.toJS(this);
      delete copy.parent;
      return copy; 
    };
    

    Other ways to handle it:

    1- don’t actually store your parent on the child object and just reference it in any handlers directly based on the argument passed to the constructor.

    var Partner = function (parent) {
        var self = this;
        self.name = ko.observable(name);
    
        self.doSomething = function() {
            //can use "parent" directly here without storing it anywhere
        };
    };
    

    2- “hide” your parent reference

    var Partner = function (parent) {
        var self = this;
        self.meta = function() {};
        self.meta.parent = parent;
        self.name = ko.observable(name);    
    };
    

    You can place your parent value behind a function (could be as a sub-property of an observable). When your structure is turned into a plain JS object by ko.toJS, then any sub-properties of functions are lost.

    3- change the structure of your application, depending on what you want to do. Suppose that the parent wants to react whenever a child has it’s name changed. You could pass in a callback, setup the subscription, and execute it whenever it changes

    var Partner = function (nameChangedCallback) {
        var self = this;
        self.name = ko.observable(name);    
    
        if (typeof nameChangedCallback == "function") {
            self.name.subscribe(function() {
               nameChangedCallback.call(self, self);
            });
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to .net and have this problem: I want to make installer
Very odd problem as this is working perfectly on our old Classic ASP site.
I'm working with ordered data and ran into this problem: I add items to
I've got an interesting problem. I recently started working at a new company that's
I had a problem working with the image classes in java. I am creating
I have a strange problem working with HTML,CSS in different browsers: Firefox 3.6 and
Problem When working with MasterPages, a common irritation I run into is that script
The Problem: I am working on a WordPress Symfony bundle and creating entities for
I'm working on problem four of Project Euler and am running into a stackoverflow
I'm working on problem 9 in Project Euler: There exists exactly one Pythagorean triplet

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.