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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:39:43+00:00 2026-06-07T10:39:43+00:00

I have a JSON file that will create my ParentModel as well as populate

  • 0

I have a JSON file that will create my ParentModel as well as populate the child Records collection.

ParentModel : Backbone.Model.extend({
  initialize: function() {
    this.set({ records: new Records(this.get("records")) });
  }
});

And the Records collection is just a basic Backbone collection that maps to a Record model.

The problem is that I need the child to know about the parent, so each Record model has to have a parent property on it. So for now I’ve just been adding this to the bottom of the initialize method:

var self = this;
this.get("records").each(function(record) {
  record.set("parent", self);
});

This works fine, but when I’m creating a new record I’d rather not have to remember to include those 4 lines.

This answer says I can override the initialize method to take in additional parameters, but I’m not quite sure how I would get Backbone to automatically pass in the ParentModel to the overridden initialize method. Can anyone provide an example on how to do that?

I’ve heard of Backbone-relational which might help do what I want, but that’s another 23kb to include. If that’s the better way to go I’ll look at implementing it, but otherwise I’d prefer a simpler solution if one is available.

This needs to work whether I create a new ParentModel record through code, or if it’s being automatically created by a JSON feed.

  • 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-07T10:39:45+00:00Added an answer on June 7, 2026 at 10:39 am

    I usually find that moving the structural elements out of the attributes is cleaner so my records and parent properties are on the object, not the attributes. That said, you could take advantage of the different events on the collection and the parent object:

    var ParentModel = Backbone.Model.extend({
        initialize: function () {
            _.bindAll(this, 'adoptOne', 'adoptAll');
            this.records = new Records();
    
            this.on('change:records', function () {
                 this.records.reset(this.get('records'));
            });
            this.records.on('reset', this.adoptAll);
            this.records.on('add', this.adoptOne);
    
            this.records.reset(this.get('records'));
        },
    
        adoptAll: function () {
           this.records.each(this.adoptOne);
        },
        adoptOne: function (model) {
            model.parent = this;
        }
    });
    

    A few tests :

    var p = new ParentModel({
        name: "I am the parent",
        records: [{id: 1}, {id: 2}]
    });
    
    p.records.add({id: 3});
    
    p.records.each(function (child) {
        console.log(child.get('id')+' '+child.parent.get('name'));
    });
    
    p.set({records: [{id: 4}]});
    
    p.records.each(function (child) {
        console.log(child.get('id')+' '+child.parent.get('name'));
    });
    

    And a Fiddle http://jsfiddle.net/sPXaZ/

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

Sidebar

Related Questions

I have a json file that I'm trying to get values out of. One
I have a JSON file that has more name value pairs than I care
I have a generated JSON file that I would like to transform. Is there
I have a json file ( ~3Gb ) that I need to load into
I have a large JSON file that contains A LOT of similar code. It's
I have a json file and I assume that I do not know anyting
I have a PHP file that returns a JSON array. I need to extract
I have a text file that contains cached data in JSON format. I'm trying
If i have a php file that outputs json data with numerical keys, like
I have this swf (flash) file that provides the json that needs to be

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.