PaulUithol/Backbone-relational — Issue #41: Getting back empty array when I need an empty Backbone.Collection
Author: cjroebuck
Created: 2011-10-14 02:25:37+00:00
State: open

I have a regular Backbone.Collection called `Contests` and a Backbone.RelationalModel called `Contest` which has the following relation to a collection of `Submission`'s:

Contest = Backbone.RelationalModel.extend
relations: [{
type: Backbone.HasMany
key: '_submissions'
relatedModel: 'Submission'
collectionType: 'SubmissionCollection'
includeInJSON: '_id'
reverseRelation:
key: 'contest'
includeInJSON: '_id'
}]
urlRoot: '/api/contests'
idAttribute: '_id'

in my app, when I do something like `contests = new Contests(); contests.fetch()` then I get back a collection of `Contest`'s which is fine. However, when I inspect the `Contest` models within this collection, the `_submissions` attribute is an empty array, instead of a `SubmissionCollection`, (i.e. an instance of a BackboneCollection). Therefore I cannot bind events etc to the collection of Submissions.

I know that Backbone-relational is doing what its supposed to do because when I type in my console `x = new Contest()` and I inspect this model, the '_submissions' attribute is an empty `SubmissionCollection`.

So what I want to know is how can I get the same behaviour when I load my Contests as part of a Contest collection.