I have the following setup (pseudo coffee code). Models and collections are loaded using Require.js.
ParentModel = Backbone.Model.extend
ParentCollection = Backbone.Collection.extend
CollectionA = ParentCollection.extend
model: ModelA
CollectionB = ParentCollection.extend
model: ModelB
CollectionC = ParentCollection.extend
model: ModelC
ModelA = ParentModel.extend
defaults:
collectionB: new CollectionB()
collectionC: new CollectionC()
ModelB = ParentModel.extend
defaults:
collectionA: new CollectionA()
ModelC = ParentModel.extend
defaults:
collectionA: new CollectionA()
ModelA has two collections with ‘child’ models. ModelB and ModelC has the vice versa: one collection with ‘parent’ models. ModelA works fine, but ModelB and ModelC generate two errors. The first by Firebug’s spy.js: “Module name ‘modelB’ has not been loaded yet for context: _” and the second by Require.js: “Module name ‘collectionB’ has not been loaded yet for context: _”. If I don’t load the collections in model B and C, there are no errors and the app works. I’m trying to solve the error, but I don’t know what is going wrong. Is it a Backbone.js circular reference issue or a Require.js circular dependency or maybe something else?
EDIT
Code for organisation.coffee (modelA)
define (require) ->
_ = require 'underscore'
mGroup = require 'models/object/group/group'
cDepartement = require 'collections/object/group/departement'
cProject = require 'collections/object/group/project'
mGroup.extend
'urlRoot': '/api/organisation'
'defaults': _.extend({}, mGroup.prototype.defaults,
'type': 'organisation'
'departements': new cDepartement()
'projects': new cProject())
Code for project.coffee (modelB)
define (require) ->
_ = require 'underscore'
mGroup = require 'models/object/group/group'
cOrganisation = require 'collections/object/group/organisation'
mGroup.extend
'urlRoot': '/api/project'
'defaults': _.extend({}, mGroup.prototype.defaults,
'type': 'project'
'organisations': new cOrganisation())
If I comment out the cOrganisation = require… and new cOrganisation than everything works. Projects, departements and organisations are all groups, but organisations are parents of projects and departements.
Yes. Just move defaults to
initializemethod. There it’ll be used when all definitions are loaded. Something like: