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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:55:41+00:00 2026-05-23T02:55:41+00:00

I’ve got the following model with nested collection var Mdl = Backbone.Model.extend({ initialize: function()

  • 0

I’ve got the following model with nested collection

var Mdl = Backbone.Model.extend({
  initialize: function() {

    // collection
    this.col1 = new NestedCollection();

  },
  ...
});

I would like to send the data for both the model and the models in the collection in one request looking something like:

{
  att1: val,
  col1: [{obj1: val}, {...}]
}

I’m unsure about the best way to hand the data in the request to the nested collection (col1). I can’t do …

var Mdl = Backbone.Model.extend({
  initialize: function() {
    // collection
    this.col1 = new NestedCollection(this.get('col1');
  },
  ...
});

… because at the time of initialize is called the parse function of the model has not been called which means that the attribute col1 is empty, another solution I thought of was to listen for the change in the parent model like…

model.bind("change:tags", function() {
  model.col1.refresh(model.get('col1'));
});

however this solution feels a little heavy handed and might potentially break any

this.col1.bind("add", function() {})

and

this.col1.bind("remove", function() {})

function set-up on the collection.

Has anyone got any idea of the ‘official’ way of doing this?

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-23T02:55:42+00:00Added an answer on May 23, 2026 at 2:55 am

    The “official” way is to override the parse method:

    http://documentcloud.github.com/backbone/#Model-parse

    In your specific case, what I would probably do is, in the parse method, build the nested collection from the col1 data, delete it from the results, then hand the results on. Backbone will then turn the rest of the data into properties.

    I have not tried this, so I’m not 100% sure it works:

    parse: function(response) {
      this.col1 = new NestedCollection(response.col1);
      delete response.col1
      return response
    }
    

    Edit: Nov 28th 2012

    Harm points out that this might not be the best way to do it any more. The original answer was written quite a while ago, and the original question indicated that the user wanted the collection to be a property on the model (not an attribute), but Harm has a point that having the collection as an attribute is a more accepted way of doing it these days.

    Today, you could use something like Backbone-Relational to handle a lot of this stuff for you, or, if you wanted to do it yourself, and have the collection as a model attribute, you could do something like:

    Building = Backbone.Model.extend({
        parse: function(response) {
            console.log("Parse Called");
            response.rooms = new Rooms(response.rooms);
            return response;
        }
    });
    Room = Backbone.Model.extend({});
    Rooms = Backbone.Collection.extend({
        model: Room
    });
    
    science_building = new Building();
    
    science_building.fetch(
        {success: function(model,resp) {console.log(resp);}}
    );
    

    With a model fetch response like:

    { id: 1, 
      name: "Einstein Hall", 
      rooms: [
        {id:101, name:'Chem Lab'},
        {id:201, name:'Physics Lab'},
        {id:205, name:'Bio Lab'}
      ]
    }
    

    Resulting in a Building model that allows:

    science_building.get('rooms').get(101).get('name')   // ==> "Chem Lab"
    

    A working jsFiddle example: http://jsfiddle.net/edwardmsmith/9bksp/

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have just tried to save a simple *.rtf file with some websites and

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.