I require Noob. When I use "require.config" and include a jQuery path with a name other than jQuery, the results are not as expected.
Here is a very simple example to help explain my problem.
File structure
root βββ Index.htm βββ scripts βββ libs β βββ jquery-1.7.1.js β βββ require.js βββ main.js βββ someModule.js
index.htm
<html> <head> <title>BackboneJS Modular app with RequireJS</title> <script data-main="scripts/main" src="scripts/libs/require.js"></script> </head> <body> <h3>BackboneJS is awesome</h3> </body> </html>
Here links to script tags are required in / libs scripts. If necessary, the JavaScript file starts with the name main.js in the scripts directory.
main.js
require.config({ "paths": { "mod1": "someModule" } }); require(["mod1"], function (sm) { console.log(sm.someValue); });
In my experience, βmod1β can be anything if it refers to the same thing in the require.config path and in the require method.
someModule.js
define([], function () { console.log(); return { someValue: "abcd" }; });
For completeness only, I have included someModule.js
Perceived inconsistency occurs when I turn on jQuery.
In the next main.js, I added jQuery to the configuration and require method.
Main.js
require.config({ "paths": { "jquery": "libs/jquery-1.7.1" ,"mod1": "someModule" } }); require(["mod1", "jquery"], function (sm, $) { console.log(sm.someValue); console.log($); });
With optional jQuery, everything seems to be still working. "Console.log ($)" writes a jQuery function.
Now the kicker. In the following code, I change "jquery" to "jqueryA" in both paths and require
require.config({ "paths": { "jqueryA": "libs/jquery-1.7.1" ,"mod1": "someModule" } }); require(["mod1", "jqueryA"], function (sm, $) { console.log(sm.someValue); console.log($); });
Now "console.log ($)" writes null.
Should this be expected? Is there a reason why the name should be jquery, but for mod1 could it be anything?
I can get around this without a problem, but this problem seems strange. I know that I can use the combined RequireJS and jQuery file, but when jQuery has an update, I donβt want it to depend on RequireJS to include the new jQuery.