2014-12-06 85 views
0

我有一個common.js定義爲RequireJS的配置:RequireJS - config;路徑和墊片不工作

(function(requirejs) { 
    "use strict"; 

    requirejs.config({ 
     baseUrl: "/js", 
     paths: { 
      "jsRoutes": "http://localhost:8080/app/jsroutes" 
     }, 
     shim: { 
      "jsRoutes": { 
       exports: "jsRoutes" 
      } 
     } 
    }); 

    requirejs.onError = function(err) { 
     console.log(err); 
    }; 
})(requirejs); 

然後我有一個main.js文件,我嘗試使用我創建的jsRoutes路徑:

require(["./common", "jsRoutes"], function (common, routes) { 
    // do something interesting 
}); 

但我不在http://localhost:8080/app/jsroutes加載資源,而是在執行main.js時嘗試加載http://localhost:8080/js/jsRoutes.js。但是這個資源不存在,我得到了一個404.

如何讓jsRoutes路徑正常工作?我也需要墊片(我不是100%確定)?

我可以調試到common.js文件,所以路徑應該被設置,對吧?

更新1

我相信路徑應工作,我讓他們定義應該不是嗎?從http://requirejs.org/docs/api.html

摘錄有可能是當你想而不是直接引用腳本符合「的baseUrl +路徑」規則找到它。如果模塊ID具有以下特徵中的一個,該ID將不通過「的baseUrl +路徑」配置過去了,只是被像普通URL,它是相對於文檔處理:

Ends in ".js". 
Starts with a "/". 
Contains an URL protocol, like "http:" or "https:". 

更新2

我可能誤解了文檔,我可以通過定義main.js像這樣解決問題:

require(["./common", "http://localhost:8080/app/jsroutes"], function (common, routes) { 
    // do something interesting 
}); 

我拉特呃希望不必傳遞這個相當笨拙的URL。

更新3

的文檔的進一步調查顯示下面的代碼片段:

requirejs.config({ 
    enforceDefine: true, 
    paths: { 
     jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min' 
    } 
}); 


//Later 
require(['jquery'], function ($) { 
    //Do something with $ here 
}, function (err) { 
    //The errback, error callback 
    //The error has a list of modules that failed 
    var failedId = err.requireModules && err.requireModules[0]; 
    if (failedId === 'jquery') { 
     //undef is function only on the global requirejs object. 
     //Use it to clear internal knowledge of jQuery. Any modules 
     //that were dependent on jQuery and in the middle of loading 
     //will not be loaded yet, they will wait until a valid jQuery 
     //does load. 
     requirejs.undef(failedId); 

     //Set the path to jQuery to local path 
     requirejs.config({ 
      paths: { 
       jquery: 'local/jquery' 
      } 
     }); 

     //Try again. Note that the above require callback 
     //with the "Do something with $ here" comment will 
      //be called if this new attempt to load jQuery succeeds. 
     require(['jquery'], function() {}); 
    } else { 
     //Some other error. Maybe show message to the user. 
    } 
}); 

這似乎這裏說的jquery路徑正在與一個完整的URL

回答

1

好吧,我意識到我做錯了。真的很簡單。

我將./commonjsRoutes的依賴項傳遞給同一個模塊,因此jsRoutes在配置之前需要使用。

我將依賴關係從main.js文件移至實際需要的位置,並按照我的預期工作。

1

我相當肯定你的路徑應該是相對於你的baseUrl。所以給它的域名&端口是搞砸了。

編輯:我的標準需要js配置...它可能有幫助嗎?

require.config({ 
    baseUrl : "./", 
    paths: { 

    // Bower Components 
    respond:  'assets/bower_components/respond/dest/respond.min', 

    // Libraries & Polyfills 
    polyfillGCS: 'assets/js/lib/polyfill-getComputedStyle', 
    polyfillRAF: 'assets/js/lib/polyfill-requestAnimationFrame', 
    polyfillPro: 'assets/js/lib/polyfill-promise', 
    easing:  'assets/js/lib/easing', 
    signalsui: 'assets/js/lib/Signals.ui', 
    signalsjs: 'assets/js/lib/Signals', 
    domReady:  'assets/js/lib/domReady', // TODO: Still needed? 

    // Modules 
    app:   'assets/js/es5/app' 

    }, 
    shim: { 
    app: { 
     deps: ['signalsjs'] 
    }, 
    signalsjs: { 
     deps: ['easing', 'polyfillGCS', 'polyfillRAF'] 
    }, 
    signalsui: { 
     deps: ['signalsjs'] 
    } 
    } 
}); 

// Load the app 
require(['app']); 
+0

我想我可以做到這一點,看我的更新1.我錯了嗎? – Neilos 2014-12-06 00:13:09

+0

請參閱頁面http://www.webjars.org/documentation上的底部代碼窗格,看起來他們正在做類似於我想實現的內容。 – Neilos 2014-12-06 00:25:34

+0

請參閱我的更新3,使用完整網址的文檔示例 – Neilos 2014-12-06 00:36:26