aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjrburke <jrburke@gmail.com>2011-04-16 20:35:18 -0700
committerjrburke <jrburke@gmail.com>2011-04-16 20:35:18 -0700
commit0b1c2e642ac0267b95759b2a7593a7d19aa43c9c (patch)
tree50e00195e48e76393fecea30e5cef145bcbf18bc
parent5d70c6d797af15641a8e562cf5b3d022a1bdda25 (diff)
downloadjquery-0b1c2e642ac0267b95759b2a7593a7d19aa43c9c.tar.gz
jquery-0b1c2e642ac0267b95759b2a7593a7d19aa43c9c.zip
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.
-rw-r--r--src/core.js14
-rw-r--r--test/data/testinit.js14
-rw-r--r--test/unit/core.js8
3 files changed, 34 insertions, 2 deletions
diff --git a/src/core.js b/src/core.js
index 89bbde5c0..9fae9ae28 100644
--- a/src/core.js
+++ b/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;
diff --git a/test/data/testinit.js b/test/data/testinit.js
index c478390d5..3b2da85fd 100644
--- a/test/data/testinit.js
+++ b/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.
diff --git a/test/unit/core.js b/test/unit/core.js
index 8d29575a5..94771649e 100644
--- a/test/unit/core.js
+++ b/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++;