0

I want to use a recursive Model/Collection structure to model a tree. I am struggling to get the circular reference to work with RequireJS. I use the CommonJs syntax rather than AMD. I get the following error:

Uncaught Error: Module name "Models/TestModel" has not been loaded yet for context: _

TestModel

define(function (require) {
    var

    // Dependencies.
    Backbone = require('backbone'),
    TestCollection = require('Collections/TestCollection'),

    TestModel = Backbone.Model.extend({
        initialize: function (model) {
            this.Children = new TestCollection(model.Children);
        }
    });

    return TestModel;
});

TestCollection

define(function (require) {

    var Backbone = require('backbone'),
        TestModel = require('Models/TestModel'),

        TestCollection = Backbone.Collection.extend({
            model: TestModel
        });

    return TestCollection;
});

Is this possible with Backbone and RequireJS?

theringostarrs
  • 11,940
  • 14
  • 50
  • 63
  • Possible duplicate of [Circular Dependency in Backbone / RequireJS Nested List](http://stackoverflow.com/questions/14324535/circular-dependency-in-backbone-requirejs-nested-list) – Louis Feb 24 '16 at 17:44
  • I've tried that - it doesn't work. I'm using CommonJs syntax rather than AMD so there is that difference too. – theringostarrs Feb 25 '16 at 09:52
  • You did not read the Q&A there carefully. It pays to read everything. The question mentions [this part of the RequrieJS documentation](http://requirejs.org/docs/api.html#circular).If you read it, you'll see examples of circular dependencies that use the CommonJS idiom. Once you understand the principles, then which idiom you use does not matter. – Louis Feb 25 '16 at 11:08
  • But the answer given doesn't mention the CommonJs idiom does it? – theringostarrs Feb 25 '16 at 11:20

0 Answers0