DzemalBecirevic/Ria — Issue #2: Great project! But how do you handle circular dependencies?
Author: amccloud
Created: 2012-03-07 14:18:47+00:00
State: open

This isn't an issue per se. I've built a similar project with requirejs and backbone-relational and I think this project is subject to one of the issues i've run into. When defining the models relations and more specifically reverse relations collection you'll run into circular dependencies that requirejs cannot handle.

```javascript

define('models/user', function(require) {
var Backbone = require('backbone');

return Backbone.RelationalModel.extend({});
});

define('collections/users', function(require) {
var Backbone = require('backbone');

return Backbone.Collection.extend({
model: require('models/user')
});
});

define('models/todo', function(require) {
var Backbone = require('backbone');

return Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasOne,
key: 'user',
relatedModel: require('models/user'),
reverseRelation: {
key: 'todos',
collectionType: require('collections/todos') // Circular
}
}]
});
});

define('collections/todos', function(require) {
var Backbone = require('backbone');

return Backbone.Collection.extend({
model: require('models/todo') // Circular
});
});
```

requirejs can make circular dependencies immediately available through exports but that doesn't play nice with backbone's factory functions. I should mention you'll also run into this issue using the alternative define syntax that is used in this project.

If you or when you or anyone else runs into this issue i'd love to work with you to fix it.