2014-04-24 34 views
0

在requirejs,我們可以通過這個爲JS設置名稱:requireJS與文件上傳插件

requirejs.config({ 
    paths: { 
     'jquery': '//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js' 
    } 
}); 

而與此使用它:

requirejs(['jquery'],function ($) { 
    //loaded and can be used here now. 
}); 

但對於像文件上傳一些插件需要大量的js文件。他們的文檔顯示瞭如何將它與requirejs一起使用:https://github.com/blueimp/jQuery-File-Upload/wiki/How-to-use-jQuery-File-Upload-with-RequireJS

但是,我該如何設法將它們全部歸爲一個名稱?

requirejs(['jquery','fileupload'],function ($) { 
    //all fileupload js has been loaded and ready to use 
}); 

因此,當我需要fileupload時,它會加載所有需要的js而不需要他們逐一。

謝謝。

回答

0

您可以創建一個元模塊導入所有必要的資源,並將它們捆綁在一起,最終的模塊則成爲被隱式加載的傳遞依賴:

fileupload.js

define(['jquery', 
     'js/jquery.iframe-transport', 
     'js/jquery.fileupload-ui' 
], function() { 
    // all needed dependencies loaded 
}); 

當您需要fileupload時,確保存在所有需要的依賴關係。