需要映射:映射扩展时获取模块超时

时间:2012-11-11 15:42:42

标签: javascript map requirejs amd shim

我有一个Mediator-Sandbox库和一个建立在这个库之上的路由器扩展。

图书馆是这样构建的:

(function(window) {

  var MedSan = {};

  window.MedSan = MedSan;

})(this);

扩展名如下:

(function(_, Backbone, MedSan) {

  MedSan.Router = {};

})(_, Backbone, MedSan);

要求加载扩展程序的配置如下所示:

require.config({

    baseUrl: "javascripts",

    shim: {
        "jquery": {
            exports: "$"
        },
        "underscore": {
            exports: "_"
        },
        "backbone": {
            deps: ['jquery', 'underscore'],
            exports: "Backbone"
        },
        "Distribution": {
            exports: "Distribution"
        },
        "Distribution.Router": {
            deps: ['underscore', 'backbone', 'Distribution'],
            exports: "Distribution"
        }
    },

    /*
    map: {
        "*": {
            "Distribution": "Distribution.Router"
        }         
    },
    */


    paths: {
        "jquery": "Vendors/jquery",
        "underscore": "Vendors/underscore",
        "backbone": "Vendors/backbone",

        "Distribution": "Distribution/Distribution",
        "Distribution.Router": "Distribution/Plugins/Router"
    }

});

我现在可以使用我的完整库:

require(['Distribution.Router'], function(Distribution) {});

当我取消注释地图配置时,我可以通过这种方式使用带有路由器扩展的完整加载模块:

require(['Distribution'], function(Distribution) {});

比我得到模块超时。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

当您将Distribution映射到Distribution.Router时,您正在创建循环依赖关系,因为Distribution.Router还取决于Distribution

要解决此问题,只需在Distribution中添加覆盖Distribution.Router地图的另一行:

map: {
    "*": {
        "Distribution": "Distribution.Router"
    },
    "Distribution.Router": {
        "Distribution": "Distribution"
    }
}