javascript - RequireJS incorrectly loads scripts from URLs -


i'm using requirejs load dependencies. how config looks like:

'use strict';  require.config({     paths: {         jquery: 'http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min',         underscore: 'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min',         backbone: 'http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min'     }     shim: {         jquery: {             exports: '$'         },         underscore: {             exports: '_'         },         backbone: {             deps: [                 'underscore',                 'jquery'             ],             exports: 'backbone'         }     } }); 

when run static website, in console there messages this:

get http://*myhost*/js/backbone.js 404 (not found) require.js:1896 uncaught error: script error for: backbone http://requirejs.org/docs/errors.html#scripterror require.js:166 http://*myhost*/js/jquery.js 404 (not found) require.js:1896 uncaught error: script error for: jquery http://requirejs.org/docs/errors.html#scripterror require.js:166 http://*myhost*/js/underscore.js 404 (not found) require.js:1896 uncaught error: script error for: underscore http://requirejs.org/docs/errors.html#scripterror require.js:166 uncaught referenceerror: jquery not defined  

as can see, requirejs ignores fact i'm providing urls cnd , tries modules locally.

however, requirejs works fine - loads modules urls. kind of lottery on there. worth mention happens on remote development server - when access website on web. when run website locally, requirejs loads modules cdn fine. weird, ideas why happening?

update

this how start application:

<script type="text/javascript" data-main="js/main" src="js/require.js"></script> 

main.js (where config loaded)

define(['config', 'router'], function(config, router) {     var router = new router();     backbone.history.start(); }); 

your main module starts this:

define(['config', 'router'], function(config, router) { 

this tells requirejs load config , router not specify order in should loaded. if router depends on modules use cdns you've listed in configuration , assuming config not among router's dependencies, indeed intermittent loading failures. when config happens load before router fine. if config loads after router, hell breaks loose. simplest way fix problem move configuration main.js file. have require.config call before define call, , you'd no longer list config among dependencies.

if moving call require.config not work because (for instance) need share config among multiple pages trick:

define(['config'], function () {     require(['router'], function(router) {         var router = new router();         backbone.history.start();     }); }); 

the code above ensures config loaded before else.

your configuration wrong in following ways:

  1. you must refer jquery jquery because jquery (for better or worse) hardcodes module name jquery lower caps. i've run tests using jquery instead of jquery , got erratic results.

  2. jquery 2.0.3 not need shim. having shim confuses requirejs.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -