I've been trying to follow the example here: Creating nested models with backboneJS + backbone-relational + requireJS
And ended up having something different (since I need a Backbone.Collection, not a Backbone.RelationalModel):
define(['exports', 'backbone', 'backbone.relational'], function (Exports, Backbone) {
var ModuleModel = Backbone.RelationalModel.extend({
relations : [{
type : Backbone.HasMany,
key : "children",
relatedModel : 'ModuleModel',
collectionType : 'ModuleCollection'
}]
});
Exports.ModuleModel = ModuleModel;
return ModuleModel;
});
define(['exports', 'backbone'], function(Exports, Backbone) {
var ModuleCollection = Backbone.Collection.extend({
model : Exports.ModuleModel,
});
Exports.ModuleCollection = ModuleCollection;
return ModuleCollection;
});
But, when I do:
var modules = new ModuleCollection();
modules.fetch();
I get:
TypeError: this.model is undefined
And it's kind of obvious since Exports.ModuleModel is created after ModuleCollection has been initialized.
Any hints on how to achieve this self-referential model?
Thanks in advance!