X
X
xaseros2015-06-20 20:34:58
JavaScript
xaseros, 2015-06-20 20:34:58

How to include RequireJS cross dependencies?

There are 2 entities Task (Task) and Stage (This). Accordingly, they are expressed by 2 backbone models.
The project uses RequireJS to load models. Models are declared like this.

define('model/stage', [
    'common/model',
    'model/task'
], function(
    Common,
    Task
){
    var Stage = {};
    
    Stage.Model = Common.Model.extend({
        
    });
    
    Stage.Collection = Common.Collection.extend({
        url: function(){
            return this.task.url() + "/stages";
        },
        task: null,
        initialize: function(models, options){
            if(!options || !options.task || ! (options.task instanceof Task.Model)){
                throw "Task model must be set";
            }
            this.task = options.task;
        }
    });
    
    return Stage;
});

and
define('model/task', [
    'common/model',
    'model/stage'
],
function(
    Common,
    Stage
){
    
    var Task = {};
    
    Task.Model = Common.Model.extend({
        setters:{
            stages: function(value, options){
                return (this.get('stages') && this.get('stages').set(value)) || (new Stage.Collection(value, {task:this}));
            }
        }
    });
    
    Task.Collection = Common.Collection.extend({
        url: UnitteCfg.restUrl+"/tasks",
        model:Task.Model
    });
    
    return Task;

});

As you can see from the code, both components have opposite dependencies. Task stores the Stage collection as an attribute, and for Stage it is important to get an instance of Task in order to check whether it is exposed.
Task is loaded first because it is listed as a dependency on another component. However, when the Stage is loaded, what goes into the closure passed in the third argument to require? why is that undefined
define('model/stage', [
    'common/model',
    'model/task'
], function(
    Common,
    Task   // <- вот это почему-то undefined
){
. . .

well and accordingly on Stage an error that Task.Model does not exist.
In principle, it is understandable, because when Require collects dependencies, it does not know at all what will be in these components, and, of course, cannot pass a component that has not yet been initialized to another component that requested it.
But what about in such a situation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xaseros, 2015-06-20
@xaseros

If anyone needs it, here is the dock
requirejs.org/docs/api.html#circular
Or the solution for the specific case when the Task is required at the execution stage, and not at the declaration stage, is to declare the name of the Task in the closure, and write to it entrust the element of the required component to the same require

define('model/stage', [
    'common/model'
], function(
    Common
){
    
    var Stage = {};
    
    var Task; // объявляем имя в замыкании
    
    require(['model/task'],function(module){ // просим загрузить нам указанный модуль, когда появится возможность
        Task = module;  // записываем в объявленное имя наш полученный модуль, но само собой, нет гарантии когда это произойдет
    });

. . .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question