RequireJS:何时使用'paths'与'packages'

Jsu*_*alv 9 javascript frontend amd requirejs

什么时候应使用pathspackages在RequireJS?是否有最好的做法,或者在我应该考虑使用其中一个的特定时间?

我跟着文档,我想出了这个:

// main.js
requirejs.config({
    enforceDefine: true,
    urlArgs: "bust=" + (new Date()).getTime(),
    baseUrl: "./js",
    waitSeconds: 7,
    paths: {
        "jquery":     [
                        'jquery'
                      ],
        "underscore": [
                        'underscore'
                      ],
        "backbone":   [
                        'backbone'
                      ],
        "handlebars":     [
                        'handlebars'
                      ]
    },
    shim: {
        "underscore": {
            deps: [],
            exports: "_"
        },
        "backbone": {
            deps: ["jquery", "underscore"],
            exports: "Backbone"
        },
        "handlebars": {
            deps: [],
            exports: "Handlebars"
        }
    } // End shim

}); // End config


// List all files; use 'define()' and not 'require()' because of shim
define([
    'jquery',
    'underscore',
    'backbone',
    'handlebars'
], function ($, _, Backbone, Handlebars)
   {
       console.log("$: " + typeof $);
       console.log("_: " + typeof _);
       console.log("Backbone: " + typeof Backbone);
       console.log("Handlebars: " + typeof Handlebars);
   }
); // End define
Run Code Online (Sandbox Code Playgroud)

但是,我观看了Jesse Warden的视频(http://css.dzone.com/articles/video-basics-requirejs),他似乎在他的大部分代码中使用了这种风格:

// main.js
requirejs.config({
    urlArgs: "bust=" + (new Date()).getTime(),
    baseUrl: "./js",
    waitSeconds: 7,
    packages: [
                'main',
                {
                    name: 'jquery',
                    location: 'libs/jquery',
                    main: 'jquery'
                },
                {
                    name: 'underscore',
                    location: 'libs/underscore',
                    main: 'underscore'
                },
                {
                    name: 'backbone',
                    location: 'libs/backbone',
                    main: 'backbone'
                },
                {
                    name: 'handlebars',
                    location: 'libs/handlebars',
                    main: 'handlebars'
                }
    ]
}); // End config
Run Code Online (Sandbox Code Playgroud)

那么这是正确的方法呢?我应该使用pathspackages?此外,还有一个modules配置.我modules什么时候使用?

Mih*_* B. 9

这个词packages指的是标准的CommonJS,因为requirejs支持加载CommonJS Packages目录结构中的模块,模块本身应该是RequireJS可以理解的模块格式.

路径配置可以是目录和文件(.js,requirejs模块).有点混乱,因为如你所说,你可以使用包来加载非标准的CommonJS包.

我什么时候使用模块?

在其中声明的requirejs中的所有内容:define('name', callback); 是一个模块

希望这个答案有所帮助