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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:35:53+00:00 2026-06-01T03:35:53+00:00

I have two backbone models, loaded from server: var Model = Backbone.Model.extend({}); var SubModel

  • 0

I have two backbone models, loaded from server:

var Model = Backbone.Model.extend({});
var SubModel = Backbone.Model.extend({});

var SubCollection = Backbone.Collection.extend({
    model: SubModel
});

var m = new Model();
m.fetch({success: function(model)
{
    model.submodels = new SubCollection();
    model.submodels.url = "/sub/" + model.get("id");
    model.submodels.fetch();
}});

So, the server has to send two separate responses. For example:

{ name: "Model1", id: 1 } // For Model fetch

and

[{ name: "Submodel1", id: 1 }, { name: "Submodel2", id: 2 }] // For Submodel collection fetch

Is there a way to fetch a Model instance with Submodel collection at once, like:

{ 
  name: "Model1", 
  id: 1, 
  submodels: [{ name: "Submodel1", id: 2 }, { name: "Submodel1", id: 2 }]
}
  • 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-01T03:35:55+00:00Added an answer on June 1, 2026 at 3:35 am

    To be able to do that is up to your back-end – it doesn’t really have anything to do with Backbone.

    Can you configure your back-end technology to return related models as nested resources?

    If your back-end is Rails, for instance, and your models are related in ActiveRecord, one way of doing this is something like

    respond_to do |format|
      format.json  { render :json => @model.to_json(:include => [:submodels])}
    end
    

    What back-end technology are you using?

    Edit:

    Sorry, misunderstood the gist of your question, once you’ve got your back-end returning the JSON in the proper format, yeah, there are things you need to do in Backbone to be able to handle it.

    Backbone-Relational

    One way to deal with it is to use Backbone-Relational, a plugin for handling related models.

    You define related models through a ‘relations’ property:

    SubModel = Backbone.RelationalModel.extend({});
    
    SubCollection = Backbone.Collection.extend({
        model: SubModel
    });
    
    Model = Backbone.RelationalModel.extend({
      relations: [
        {
            type: 'HasMany',
            key: 'submodels',
            relatedModel: 'SubModel',
            collectionType: 'SubCollection'
        }
      ]
    });
    

    When your Model fetches the JSON, it will automatically create a SubCollection under the ‘submodels’ property and populate it with SubModels – one for each JSON object in the array.

    jsfiddle for backbone-relational: http://jsfiddle.net/4Zx5X/12/

    By Hand

    You can do this by hand if you want as well. In involves overriding the parse function for your Model class (forgive me if my JS is not 100% correct – been doing CoffeeScript so much lately its hardwired in my brain)

    var Model = Backbone.Model.extend({
      parse: function(response) {
        this.submodels = new SubCollection();
        // Populate your submodels with the data from the response.
        // Could also use .add() if you wanted events for each one.
        this.submodels.reset(response.submodels);
        // now that we've handled that data, delete it
        delete response.submodels;
        // return the rest of the data to be handled by Backbone normally.
        return response;
      }
    });
    

    parse() runs before initialize() and before the attributes hash is set up, so you can’t access model.attributes, and model.set() fails, so we have to set the collection as a direct property of the model, and not as a “property” that you would access with get/set.

    Depending on what you want to happen on “save()” you may have to override `toJSON’ to get your serialized version of the model to look like what your API expects.

    jsfiddle:

    http://jsfiddle.net/QEdmB/44/

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

Sidebar

Related Questions

Here I have a Backbone.js Model - Contact and a Collection - Contacts with
I have two classes generated by LINQ2SQL both from the same table so they
I have two queries, as following: SELECT SQL_CALC_FOUND_ROWS Id, Name FROM my_table WHERE Name
I have noticed that when multiple attributes of a Backbone model are set like
Newbie backbone question: Context: Building a shopping list with backbone I have a model
I have encountered a strange behaviour of the model validation in Backbone.js. When a
I have two backbone Views. One is the main view and the other is
I have a model that has a bunch of attributes but the two of
Taking the following Model: MyModel= Backbone.Model.extend({ defaults : { myNestedModel:undefined, }, initialize: function() {
We have two servers in the same network. One of them is the server

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.