Why is it not necessary to forward jQuery to JJ objects as a function alias?

I uploaded a sample project for the latest version of RequireJS . Their documentation implies anything downloaded is passed to the list of parameters of the associated function (in the appropriate order).

So, I decided to try ... but it doesn't seem to work!

  • Firebug (net tab) shows jQuery as downloadable: so RequireJS obvioulsy successfully completed this part
  • Firebug (console tab) shows that '$ is not a function'

My question is: Why is the nickname not populated?

MY CODE LOOKS LIKE:

 <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/require.js" type="text/javascript"></script> <script type="text/javascript"> require(["scripts/jQuery/Core/jquery-1.7.2.min"], function ($) { // jQuery is not passed-into the function, so the alias fails! $(function () { var stop = ""; }); }); </script> </head> <body> <form id="form1" runat="server"> </form> </body> </html> 

THEME SAMPLES SEE HOW:

 //Inside scripts/main.js require(["some/module", "a.js", "b.js"], function(someModule) { //... }); 
+7
source share
3 answers

jQuery must be loaded via the special name "jquery", otherwise it will not be registered (since jQuery uses a named definition).

 // create an alias that points to proper file require.config({ paths : { jquery : "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min" } }); // require jquery usign the path config require(["jquery"], function ($) { console.log($); }); 

Here are the main reasons why named define is considered an anti-pattern and should only be used when it is needed (when you have several modules inside the same file).

+16
source

Be sure to read " README.md " for the RequireJS + jQuery sample project. There are many complications with using jQuery that you need to decide how best to solve it for your project during the initial setup. Once you have found out what is best for you and implemented it, this should not be a problem again.

Most of the complications are also related to the fact that jQuery is not a true AMD module, they just have a hack in the code base to determine if the function detects the definition. For example, this means that the jQuery module name will always be "jquery" (note the lowercase "q") if you do not wrap it yourself, so if you specify a path for it in your configuration, you MUST have a key with the name "jquery" (again, with lowercase "q") or there will be a mismatch. This is a bit of us when setting up RequireJS for the first time in our project (we named the key "jQuery").

+2
source

Is your script a "Scripts" or "scripts" folder?

You make a request for "Scripts / require.js" as well as for "scripts / jQuery / Core / jquery-1.7.2.min". Take a look at the Net Firebug tab ... I bet you create 404 there.

0
source

All Articles