2014-12-06 64 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