Answer the question
In order to leave comments, you need to log in
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;
});
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;
});
define('model/stage', [
'common/model',
'model/task'
], function(
Common,
Task // <- вот это почему-то undefined
){
. . .
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question