2012-12-08 64 views
0

我正在學習如何使用require.js。我想這樣定義模塊:使用require.js進行跨域定義

define('presenter', ['jquery']....... 

的問題是,我的主頁是不同的域在的「http://本地主機:62607 /」,所以我得到的演示看有一個404錯誤。

演示者實際上位於此處:'http:// localhost:62588/scripts/app/presenter'。

所以,如果我定義演示,如:

define('http://localhost:62588/scripts/app/presenter', ['jquery'], 

一切工作正常,但我更喜歡更具可讀性的第一個版本。

有什麼可以做到這一點?

感謝

回答

2

您可以在requireJS配置使用baseUrlpath性能實現這一目標。

從requireJS文檔:

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

如果你定義一個名爲app模塊,requireJS將尋找它在../app

+0

謝謝。正是我需要的。只需包含baseUrl並記住並刪除.js。例如。當引用prenter.js時。 – davy

相關問題