RequireJS:相互递归模块的问题

时间:2012-06-23 19:48:39

标签: javascript backbone.js requirejs parse-platform

我有一个Workout对象和一个WorkoutSection对象。两者都使用另一个属性。 Workout在加载过程中未使用WorkoutSection,但WorkoutSection在加载过程中使用Workout

WorkoutSection.js

define(['require',
    // post-load
    'models/Workout'
], 
function(require) {
    // must require Workout because of mutual dependency
    var Workout = require('models/Workout');

Workout.js

define([
    'require','models/WorkoutSection'
], 

    function(require) {
        // must re-require Workout because of mutual dependency
        var WorkoutSection;

        var Workout = Parse.Object.extend("Workout",
        {
            initialize : function() {
                WorkoutSection = require('models/WorkoutSection');
            },

错误:

  

未捕获错误:尚未加载模块名称“models / Workout”   上下文:_ http://requirejs.org/docs/errors.html#notloaded   require.js:2 H require.js:2 k.s.newContext.j.require require.js:2   requirejs require.js:2(匿名函数)WorkoutSection.js:20

我正在按照链接中描述的解决方案,但我仍然得到错误= S任何可以解决此问题的想法?

这是我的main.js:

// Filename: main.js

// Require.js allows us to configure shortcut alias
// Their usage will become more apparent futher along in the tutorial.
require.config( {
    paths : {
        jQuery : 'libs/jquery/jquery-min',
        Underscore : 'libs/underscore/underscore-min',
        Backbone : 'libs/backbone/backbone-min',
        Parse : 'libs/parse/parse-min',
        templates : '../templates'
    }
});

require( [
    // Load our app module and pass it to our definition function
    'app',
],
function(App) {
    // The "app" dependency is passed in as "App"
    // Again, the other dependencies passed in are not "AMD" therefore
    // don't pass a parameter to this function
    App.initialize();
});

谢谢!

1 个答案:

答案 0 :(得分:1)

我建议你:

  1. 尝试将WorkoutSection.js重新加工为CommonJS格式,如下所示:http://requirejs.org/docs/api.html#cjsmodule

  2. 完全排除models/WorkoutSection作为Workout.js的依赖关系