2012-11-19 153 views
4

我建設有requireJs我的文件結構的項目多文件庫如下:如何使用與requireJs

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 /佈局/ top.js',等]

  • 創建某種嗎?requireJs插件/模塊

感謝

回答

3

最後,我成功地實施第三方案:我創建了一個網絡模塊,包庫中的文件名爲notiy.js:

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:

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){ 
//... 
}); 

答它就像一個魅力!

+0

不起作用。佈局和主題js文件都依賴於jquery和noty,這些deps在這裏沒有定義。 – Fdr

+0

jquery和noty在'js/lib'下,所以不需要定義它們 – krampstudio

3

或像這樣:

paths: { 
    'jquery': 'jquery/1.10.2/jquery', 
    'noty': 'noty/2.0/jquery.noty', 
    'noty.themes.default': 'noty/2.0/themes/default', 
    'noty.layouts.topCenter': 'noty/2.0/layouts/topCenter', 

    app: '../app', 
    util: '../util' 
}, 
shim: { 
    'noty': ['jquery'], 
    'noty.themes.default': { 
     deps: ['jquery', 'noty'], 
     exports: 'jquery' 
    }, 
    'noty.layouts.topCenter': { 
     deps: ['jquery', 'noty'], 
     exports: 'jquery' 
    } 
}