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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:02:01+00:00 2026-06-05T23:02:01+00:00

I see there are techniques available to have deep models in Backbone, but how

  • 0

I see there are techniques available to have deep models in Backbone, but how about hierachical collections of a single model?

An obvious example is a category tree. So, we can have a category model which has a few properties, ‘name’, ‘type’, ‘color’, whatever.

Rather than have relational db style parent ids, we’re using js so we want the data to be represented as json.

Can backbone be leveraged to support (presumably by augmenting collections) data structures that contain instances of a single model in a tree (such that the models and sub models are all instances of the same model)?

  • 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-05T23:02:03+00:00Added an answer on June 5, 2026 at 11:02 pm

    Have a look at Backbone-Relational or supermodel.js.

    These projects provide better forms of model nesting than the default implementation.

    We just nest Backbone models like:

    var MyModel = Backbone.Model.extend({});
    var MySubModel = Backbone.Model.extend({});
    
    var model = new MyModel({submodel: new MySubModel({color: 'blue'})});
    

    And we override the toJSON methods:

    // nested models!  Might just override the internal representation of this...
    _.extend(Backbone.Model.prototype, {
      // Version of toJSON that traverses nested models
      toJSON: function() {
        var obj = _.clone(this.attributes);
        _.each(_.keys(obj), function(key) {
          if(!_.isUndefined(obj[key]) && !_.isNull(obj[key]) && _.isFunction(obj[key].toJSON)) {
            obj[key] = obj[key].toJSON();
          }
        });
        return obj;
      }
    });
    
    _.extend(Backbone.Collection.prototype, {
      // Version of toJSON that traverses nested models
      toJSON: function() {
        return this.map(function(model){ return model.toJSON(); });
      }
    });
    

    So the JSON representations look correct when we nest models. You’ll have to pay attention to the parse method on your model though – when you get back your JSON from the server you’re going to have to generate all the submodels and collections there in order for it all to work correctly.

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

Sidebar

Related Questions

I see there is a question here but there is no definite answer. Has
I see there is version 1.5 and 3.0 beta , but I can't seem
I see there are BN_CLICKED and BN_DBLCLK notification messages for a button control. but
I see there is a API call for Frienships/Show, but I am not sure
I see there is full ajax mode but I wanted to see if there
I see there is an extension for Ninject integration with asp.net-mvc but it looks
i have written the following code: As you can see there is a for
There are many papers about ranged combat artificial intelligences, like Killzones's ( see this
I see there are lot's of examples in Ext JS where instead of actually
I can see there are a lot of questions for getting the number of

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.