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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:12:21+00:00 2026-06-13T17:12:21+00:00

I have a backbone collection on the client. Model of the collection has some

  • 0

I have a backbone collection on the client.
Model of the collection has some properties along with another collection

When I do fetch() my action method on the server returns some data, collection gets populated, all the properties too, except that nested collection.

What could be the reason?

var Job = Backbone.Model.extend();
var Jobs = Backbone.Collection.extend({model: Job})

var Foo = Backbone.Model.extend({ 
    initialize:function(){
      this.jobs = new Jobs();
}})
var FooCollection = Backbone.Collection.extend({model: Foo})

var fooCol = new FooCollection()
fooCol.fetch();

fooCol.first().get('name') // => returns name
fooCol.first().jobs.toJSON() // returns nothing
// although this will
fooCol.first().get('jobs') //it will return an array

So somehow nested Backbone collection becomes just a regular property (Array)

  • 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-13T17:12:23+00:00Added an answer on June 13, 2026 at 5:12 pm

    OK – with your extra information, I can give you an answer.

    First – “get” doesn’t get a property off of the model. It gets a property off of the model’s attributes property. So, the attributes probably look like:

    {
        name: 'blah',
        jobs: [{name: 'job1'}, {name: 'job2'}]
    }
    

    Backbone doesn’t automagically transform arrays into collections and models, and simply setting this.jobs isn’t going to work. What you need to do is a little more complex.

    var Foo = Backbone.Model.extend({ 
        initialize:function(){
          this.jobs = new Jobs(this.attributes.jobs));
        }
    });
    

    This will set your ‘jobs’ property to a new jobs object with the data that was sent over for the jobs. But, alas, it won’t automatically fire events on the Jobs collection, nor will it allow you to use helpers like this.get('jobs').each(fn); – you’ll only be able to use it as Foo.jobs.each(fn).

    In order for you to use the attribute as an actual collection, you’ll have to do a lot more complicated things.

    var Foo = Backbone.Model.extend({ 
        initialize:function(){
          this.createJobs(this.attributes.jobs);
        },
        toJSON: function () {
            var json = Backbone.Model.prototype.toJSON.apply(this);
            json.jobs = this.get('jobs').toJSON();
            return json;
        },
        set: function (key, val) {
            var attributes;
            if(!_.isObject(key)) {
                attributes = {}; attributes[key] = val;
            } else {
                attributes = key;
            }
            safeAttributes = _.omit(attributes, 'jobs');
            Backbone.Model.prototype.set.call(this, safeAttributes);
            if(attributes.jobs) { this.get('jobs').reset(attributes.jobs); }
        },
        clear: function () {
            if(this.get('jobs') && this.get('jobs').destroy) {
                this.get('jobs').off(); 
                this.get('jobs').destroy(); 
            }
            Backbone.Model.prototype.clear.apply(this);
            this.createJobs();
        },
        createJobs: function (jobsArray) {
            var jobsCollection = new Jobs(jobsArray);
            jobsCollection.on('change', function () {this.trigger('change'); }, this);
            this.set('jobs', jobsCollection);
        }
    });
    

    Note that this is completely untested, but hopefully it shows some of the way you’d do this.

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

Sidebar

Related Questions

I have a Backbone View that has a Collection as its Model. If the
I use backbone.js and have a model without a collection. In the view I
let's say I have : var Book = Backbone.Model.extend(); var Collection = Backbone.Collection.extend({ model:
I have the following Backbone.js model and collection: // Model lr.Event = Backbone.Model.extend({}); //
I have very simple backbone model and collection. I have a corresponding backbone.marionette.CollectionView and
My Backbone Collection receives 30 models on fetch(). I have tried newColl=origColl.first(2); to return
I have a Backbone collection and when I add a new model to it
I have a Backbone collection model (with sub-models as elements) and views to edit
I have a backbone collection and when I remove a model from the collection,
I have a Backbone.js Collection and I have an array of model IDs that

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.