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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:30:55+00:00 2026-05-24T12:30:55+00:00

I have a large json server response which nests models in models. I have

  • 0

I have a large json server response which nests models in models. I have no way of making the server rest based. I need to parse the json tree into collections and collections within collections. I would then like to export the backbone models in the same json structure to the server after modification.

i have a collection called sections, and using this method to nest the questions collection within each section model:
http://documentcloud.github.com/backbone/#FAQ-nested

the top level of the json response is the array of sections so i have been able to pass that directly to the sections collection, I have then used the initialize method to parse out the child questions and then deleted them from the attributes. But this means i dont get the model.questions returned in any toJSON on the sections collection.

SCC.Models.Section = Backbone.Model.extend({
    initialize: function() {
        var questions = this.get('questions');
        this.questions = new SCC.Collections.Questions(questions);
        delete this.attributes.questions;

    }
});

SCC.Collections.Sections = Backbone.Collection.extend({
    model: SCC.Models.Section
});

SCC.Sections = new SCC.Collections.Sections();
//imagine window.SectionData is the server response
SCC.Sections.reset(window.SectionData);

Hopefully I am clear. Let me if you need more info.

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-05-24T12:30:56+00:00Added an answer on May 24, 2026 at 12:30 pm

    If you have a limited number of nested models which are under you control and are not subject to frequent change, the easy way is to override toJSON() method for your models that have nested collections:

    SCC.Models.Section = Backbone.Model.extend({
        initialize: function() {
            var questions = this.get('questions');
            this.questions = new SCC.Collections.Questions(questions);
            delete this.attributes.questions;
        },
        toJSON: function() {
            var json = Backbone.Model.prototype.toJSON.call(this);
            json.questions = this.questions.toJSON()
            return json
        }
    });
    

    However, if you have a lot of nested collections which are subject to change, more abstract way would be nice.

    I can think for example of storing all nested collections in model’s special property (say nestedCollections) and then monkey patching Backbone.Model.prototype.toJSON so that it will always parse all nestedCollections and add them to JSON object before returning it.

    Another way would be to use Backbone relational instead of nested models (it automatically handles toJSON so you won’t have to think about it).

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

Sidebar

Related Questions

I have a very large response from server of JSON string. I converted it
I have large json data which is contains hundreds thousands elements, now i need
I have a WCF REST Service which accepts a JSON string One of the
I have a very large integer returned by a json string from my server
I use SBJsonParser for parsing a large json response I query from a server.
So I have a large JSON object i'm returning from the server, then building
I have a couple of large json files that I need to check for
I have a large tree of Java Objects in my Desktop Application and am
I have a search that returns JSON, which I then transform into a HTML
I have a very large json like : raw_obj= {001 : {....}, 002 :

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.