Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add support for registering jQuery as an AMD module. Only does so if the #331

Closed
wants to merge 1 commit into from

4 participants

@jrburke

Add support for registering jQuery as an AMD module. Only does so if the AMD loader indicates it has special allowances for multiple versions of jQuery being loaded in a page.

The "special allowances" should be:

  • Being able to restrict registration of jQuery to a specific version.
  • Allow multiple versions of jQuery to be loaded in a page if the loader supports different "sandboxes" for defined modules.

An AMD loader needs to have these special allowances for jQuery since jQuery is commonly used by third-party libs used in pages that can load more than one version of jQuery in a page. While this conceptually could happen with any library/toolkit script, jQuery's popularity make it an actual issue, and an AMD loader needs to be able to account for it, or specifically tells its users the restrictions it has with its use and jQuery.

With this RequireJS commit, RequireJS demonstrates meeting these allowances by allowing the developer to specify the version of jQuery they want to allow registered, so if more than one jQuery with a define() call happens, RequireJS will only pay attention to the desired version. The RequireJS commit contains some unit tests to show either 1.4.4 for 1.5.2 being used depending on version set in the RequireJS config.

This helps avoid the issue of some other third party code on a page accidentally loading a version of jQuery the main page developer was not expecting and have it clobber the one that was desired.

RequireJS also supports "contexts" that act like "sandboxes" and allow the developer to load two different versions of jQuery to be loaded and used in a page, as long as jQuery uses define() to register.

RequireJS 1.0 can support this model if/when jQuery decides to accept this pull request, as long as we agree that define.amd.jQuery === true is the capability name/test. If the jQuery project prefers a different capability name, just let me know (preferably within the next week), and I can have the capability flag in the RequireJS 1.0 release.

This pull request includes a define qunit test that was originally authored by csnover, but changed to fit the current suggested capability check.

@jrburke jrburke Add support for registering jQuery as an AMD module. Only does so if …
…the AMD loader indicates it has special allowances for multiple versions of jQuery being loaded in a page.
0b1c2e6
@jeresig

For reference, this patch is related to: http://bugs.jquery.com/ticket/7102

@dmethvin
Owner

With 1.6 now in beta, should this go into our 1.7 wishlist? It would be good to get it into the 1.7 alpha early since jrburke has the pull ready and that would give everyone in the script loader community a heads-up.

@timmywil
Collaborator

Landed in commit bba3d61.

@timmywil timmywil closed this
@mescoda mescoda referenced this pull request from a commit in mescoda/jquery
@jrburke jrburke Landing pull request 331. Add support for registering jQuery as an AM…
…D module. Fixes #7102.

More Details:
 - jquery#331
 - http://bugs.jquery.com/ticket/7102
400f3cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 17, 2011
  1. @jrburke

    Add support for registering jQuery as an AMD module. Only does so if …

    jrburke authored
    …the AMD loader indicates it has special allowances for multiple versions of jQuery being loaded in a page.
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 2 deletions.
  1. +14 −0 src/core.js
  2. +13 −1 test/data/testinit.js
  3. +7 −1 test/unit/core.js
View
14 src/core.js
@@ -930,6 +930,20 @@ function doScrollCheck() {
jQuery.ready();
}
+// Expose jQuery as an AMD module, but only for AMD loaders that
+// understand the issues with loading multiple versions of jQuery
+// in a page that all might call define(). The loader will indicate
+// they have special allowances for multiple jQuery versions by
+// specifying define.amd.jQuery = true. Register as a named module,
+// since jQuery can be concatenated with other files that may use define,
+// but not use a proper concatenation script that understands anonymous
+// AMD modules. A named AMD is safest and most robust way to register.
+// Lowercase jquery is used because AMD module names are derived from
+// file names, and jQuery is normally delivered in a lowercase file name.
+if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
+ define( "jquery", [], function () { return jQuery; } );
+}
+
// Expose jQuery to the global object
return jQuery;
View
14 test/data/testinit.js
@@ -1,7 +1,19 @@
var jQuery = this.jQuery || "jQuery", // For testing .noConflict()
$ = this.$ || "$",
originaljQuery = jQuery,
- original$ = $;
+ original$ = $,
+ amdDefined;
+
+/**
+ * Set up a mock AMD define function for testing AMD registration.
+ */
+function define(name, dependencies, callback) {
+ amdDefined = callback();
+}
+
+define.amd = {
+ jQuery: true
+};
/**
* Returns an array of elements with the given IDs, eg.
View
8 test/unit/core.js
@@ -214,6 +214,12 @@ test("browser", function() {
});
}
+test("amdModule", function() {
+ expect(1);
+
+ equals( jQuery, amdDefined, "Make sure defined module matches jQuery" );
+});
+
test("noConflict", function() {
expect(7);
@@ -850,7 +856,7 @@ test("jQuery.each(Object,Function)", function() {
f[i] = "baz";
});
equals( "baz", f.foo, "Loop over a function" );
-
+
var stylesheet_count = 0;
jQuery.each(document.styleSheets, function(i){
stylesheet_count++;
Something went wrong with that request. Please try again.