2013-08-06 3 views
8
の場合は pathsをいつ使うべきですか?これにはベストプラクティスがあるのですか、それとも1つを他のものよりも優先して使用することを検討すべき特定の時間がありますか?

RequireJS:「パス」と「パッケージ」のどちらを使用するか

私はドキュメントを追っていると私はこの思い付いた:しかし、私はジェシーワーデン(http://css.dzone.com/articles/video-basics-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 

// 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 

これは適切な方法ですか? pathsまたはpackagesを使用する必要がありますか?また、modules設定があります。 modulesはいつ使用しますか?

+0

私の限られた経験から、あなたはdev jqueryでパッケージを使用する必要があります。そうでなければ、core.jsや他の依存関係をjquery /ではなくbaseUrl /からロードできません。 –

答えて

8

requirejsはCommonJS Packagesディレクトリ構造のモジュールのロードをサポートしており、モジュール自体はRequireJSが理解できるモジュール形式である必要があるため、という単語は標準CommonJSを参照しています。

パスの設定は、ディレクトリとファイル(.js、requirejsモジュール)の場合もあります。パッケージを使用して非標準のCommonJSパッケージをロードすることができますので、少し混乱します。

いつモジュールを使用しますか?内で宣言requirejsで

すべて:define('name', callback);

は、この回答がお役に立てば幸いモジュールです。

関連する問題