2012-07-20 44 views
21

我是新的RequireJS,我堅持加載順序。按照特定的順序加載文件與RequireJs

我有一個全局的項目配置,我需要在位於js/app/*中的模塊之前加載。

這裏是我的struture:

index.html 
config.js 
js/ 
    require.js 
    app/ 
     login.js 
    lib/ 
     bootstrap-2.0.4.min.js 

這裏的config.js文件:

var Project = { 
    'server': { 
     'host': '127.0.0.1', 
     'port': 8080 
    }, 
    'history': 10,  // Number of query kept in the local storage history 
    'lang': 'en',  // For future use 
}; 

這是我的requirejs文件(app.js):

requirejs.config({ 
    //By default load any module IDs from js/lib 
    baseUrl: 'js/lib', 
    //except, if the module ID starts with "app", 
    //load it from the js/app directory. paths 
    //config is relative to the baseUrl, and 
    //never includes a ".js" extension since 
    //the paths config could be for a directory. 
    paths: { 
     bootstrap: '../lib/bootstrap-2.0.4.min', 
     app: '../app', 
    }, 
    shim: { 
     'app': { 
      deps: ['../../config'], 
      exports: function (a) { 
       console.log ('loaded!'); 
       console.log (a); 
      } 
     } // Skual Config 
    }, 
}); 

var modules = []; 
modules.push('jquery'); 
modules.push('bootstrap'); 
modules.push('app/login'); 


// Start the main app logic. 
requirejs(modules, function ($) {}); 

但有些時候,當我加載頁面時,我有一個「Project」未定義,因爲login.js已經加載了BEFORE config.js。

如何強制config.js首先被加載,不管是什麼?

注意:我將orders.js看作RequireJS的一個插件,但它自v2以來顯然不受支持,替換爲shim

感謝您的幫助!

回答

14

如果您將js文件定義爲AMD模塊,則不必擔心加載順序。 (或者,如果您無法修改config.jslogin.js以致電define,則可以使用填充配置)。

config.js應該是這個樣子:

define({project: { 
    'server': { 
     'host': '127.0.0.1', 
     'port': 8080 
    }, 
    'history': 10,  // Number of query kept in the local storage history 
    'lang': 'en',  // For future use 
}}); 

login.js:

define(['jquery', '../../config'], function($, config) { 

    // here, config will be loaded 
    console.log(config.project) 

}); 

再次,應該只有當調用define()模塊裏面是不是一種選擇使用墊片的配置。

25

今天出現了類似的問題 - 我們已經引導了我們想要確保在其他任何東西之前加載的數據,並且在評估任何其他模塊之前設置暴露該數據的模塊。

我發現強制加載順序的最簡單的方法是簡單地需要一個模塊在繼續與應用程序初始化之前加載:

require(["bootstrapped-data-setup", "some-other-init-code"], function(){ 
    require(["main-app-initializer"]); 
}); 
+0

同意。我只需要在開發過程中暫時做到這一點,並且足夠好! – 2014-12-06 05:45:14

+0

甚至可以將上面的代碼放在_requirejs-config.js_中按順序加載。 – Sunry 2016-08-30 08:44:15

20

有一個可能的解決方案,以建立一個隊列被加載的模塊。在這種情況下,所有模塊將按照確切順序逐個加載:

var requireQueue = function(modules, callback) { 
    function load(queue, results) { 
    if (queue.length) { 
     require([queue.shift()], function(result) { 
     results.push(result); 
     load(queue, results); 
     }); 
    } else { 
     callback.apply(null, results); 
    } 
    } 

    load(modules, []); 
}; 

requireQueue([ 
    'app', 
    'apps/home/initialize', 
    'apps/entities/initialize', 
    'apps/cti/initialize' 
], function(App) { 
    App.start(); 
}); 
+0

有道理!謝謝。你有任何想法,如果這取決於嵌套模塊模塊的情況下工作嗎? – Sushruth 2016-12-05 00:33:42

+0

請注意,r.js優化器可能不會支付任何特定的訂單注意力,因爲應用程序在應用程序/ home之前並不意味着在最終的縮小concat文件中,應用程序的源代碼將出現在app/home之前...唯一的保證是它們都在回調之前加載 – Patrick 2017-07-31 06:57:43