4

私のファイル構造は次のとおりです。

js/
   lib/
       noty/
           layouts/
               bottom.js
               top.js
               ...
           themes/
               default.js
           noty.jquery.js
       jquery.js
       jquery-ui.js
   user/
      user.js
   app.js

そして私の設定:

    requirejs.config({
        baseUrl: 'js/lib',
        urlArgs: 'bust=' + (new Date()).getTime(),  //only for dev  : no-cache
        paths: {
            user: '../user'
        },
        shim: {
            'jquery-ui': ['jquery'],
            'jquery-tmpl': ['jquery'],
            'gridy': ['jquery']
        }
    });
    requirejs(['jquery', 'jquery-ui'],  function($){
    ....
    });

私の懸念は、任意のモジュールで使用できる通知プラグインである noty を統合しようとしていることです。このプラグインはロードする必要があります:

js/lib/noty/noty.jquery.js
js/lib/noty/layout/top.js
js/lib/noty/themes/bottom.js

それを行う方法が何であるかわかりませんか?

  • ファイルを連結しますか?

  • 各ファイルを依存関係としてロードしますか? :

    requirejs(['jquery', 'jquery-ui', 'noty/noty.jquery.js', 'noty/layout/top.js', など]

  • requireJs 用のある種のプラグイン/モジュールを作成しますか?

ありがとう

4

2 に答える 2

3

最後に、3 番目のソリューションを実装することができました。ライブラリを notiy.js という名前のファイルにラップする Web モジュールを作成しました。

define(['jquery',
    'noty/layouts/topCenter', 
    'noty/layouts/bottomRight',
    'noty/themes/default'], 
function($){

$.noty.defaults.timeout = 20000;

return  function(type, msg){
    var topLayout = 'topCenter',
        bottomLayout = 'bottomRight',
        layout = {
            'alert'     : topLayout,
            'info'      : bottomLayout,
            'confirm'   : topLayout,
            'success'   : bottomLayout,
            'error'     : topLayout,
            'warning'   : topLayout
        };

    if(msg && type){
        return noty({
            text : msg, 
            layout: layout[type] || topLayout, 
            type : type
        });
    }
}
});

app.js の shim config (依存関係の順序を修正するため) で依存関係を宣言しました。

requirejs.config({
baseUrl: 'js/lib',
urlArgs: 'bust=' + (new Date()).getTime(),  //only for dev  : no-cache
paths: {
    user: '../user'
},
shim: {
    'jquery-ui'         : ['jquery'],
    'jquery-tmpl'       : ['jquery'],
    'gridy'             : ['jquery'],
    'noty/jquery.noty'  : ['jquery'],
    'notify'            : ['noty/jquery.noty']
}
});
requirejs(['jquery', 'jquery-ui', 'jquery-tmpl', 'notify'],  function($, ui, tmpl, notify){
//...
});

Ans それは魅力のように動作します!

于 2012-11-21T11:37:10.680 に答える