一尘不染

使用RequireJS加载主干和下划线

javascript

我正在尝试使用RequireJS加载Backbone和Underscore(以及jQuery)。使用最新版本的Backbone和Underscore,似乎有些棘手。例如,Underscore自动将自身注册为模块,但Backbone假定Underscore在全球范围内可用。我还应该注意,Backbone似乎并没有将自己注册为模块,这使其与其他库有些不一致。这是我能想到的最好的main.js:

require(
{
    paths: {
        'backbone': 'libs/backbone/backbone-require',
        'templates': '../templates'
    }
},
[
    // jQuery registers itself as a module.
    'http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js',

    // Underscore registers itself as a module.
    'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.2.1/underscore-min.js'
], function() {

    // These nested require() calls are just due to how Backbone is built.  Underscore basically says if require()
    // is available then it will automatically register an "underscore" module, but it won't register underscore
    // as a global "_".  However, Backbone expects Underscore to be a global variable.  To make this work, we require
    // the Underscore module after it's been defined from within Underscore and set it as a global variable for
    // Backbone's sake.  Hopefully Backbone will soon be able to use the Underscore module directly instead of
    // assuming it's global.
    require(['underscore'], function(_) {
        window._ = _;
    });

    require([
        'order!http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js',
        'order!app'
    ], function(a, app) {
        app.initialize();
    })
});

我应该提到的是,尽管它起作用了,但优化器对此却感到窒息。我收到以下信息:

Tracing dependencies for: main
js: "/home/httpd/aahardy/requirejs/r.js", line 7619: exception from uncaught JavaScript throw: Error: Error: Error evaluating module "undefined" at location "/home/httpd/aahardy/phoenix/trunk/ui/js/../../ui-build/js/underscore.js":
JavaException: java.io.FileNotFoundException: /home/httpd/aahardy/phoenix/trunk/ui/js/../../ui-build/js/underscore.js (No such file or directory)
fileName:/home/httpd/aahardy/phoenix/trunk/ui/js/../../ui-build/js/underscore.js
lineNumber: undefined
http://requirejs.org/docs/errors.html#defineerror
In module tree:
    main

有没有更好的方法来解决这个问题?谢谢!


阅读 237

收藏
2020-05-01

共1个答案

一尘不染

现在,RequireJS2.X使用新shim配置可以更好地有机解决非AMD模块,例如Backbone&Underscore 。

shim配置易于使用:(1)声明依赖项(deps)(如果有)(可能来自paths配置,也可能是有效路径本身)。(2)(可选)从要填充的文件中指定全局变量名称,该名称应导出到需要它的模块函数中。(如果不指定导出,则只需使用全局变量,因为任何内容都不会传递到您的require/ define函数中。)

这是shim加载Backbone 的简单示例用法。即使没有任何依赖性,它也会为下划线添加导出。

require.config({
  shim: {
    underscore: {
      exports: '_'
    },
    backbone: {
      deps: ["underscore", "jquery"],
      exports: "Backbone"
    }
  }
});

//the "main" function to bootstrap your code
require(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) {   // or, you could use these deps in a separate module using define

});

注意: 此简化的代码假定jquery,bone和underscore位于与此“ main”代码相同的目录下的名为“ jquery.js”,“
backbone.js”和“ underscore.js”的文件中(这将成为require的baseURL )。如果不是这种情况,则需要使用path
config。

我个人认为,使用内置shim功能,不使用分叉版本的Backbone&Underscore的优势要胜过使用另一个流行答案中推荐的AMD叉的优势,但是无论哪种方式都能奏效。

2020-05-01