Skip to content

Commit bba3d61

Browse files
jrburketimmywil
authored andcommitted
Landing pull request 331. Add support for registering jQuery as an AMD module. Fixes #7102.
More Details: - jquery#331 - http://bugs.jquery.com/ticket/7102
2 parents 2831cfd + 0b1c2e6 commit bba3d61

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/core.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,20 @@ function doScrollCheck() {
935935
jQuery.ready();
936936
}
937937

938+
// Expose jQuery as an AMD module, but only for AMD loaders that
939+
// understand the issues with loading multiple versions of jQuery
940+
// in a page that all might call define(). The loader will indicate
941+
// they have special allowances for multiple jQuery versions by
942+
// specifying define.amd.jQuery = true. Register as a named module,
943+
// since jQuery can be concatenated with other files that may use define,
944+
// but not use a proper concatenation script that understands anonymous
945+
// AMD modules. A named AMD is safest and most robust way to register.
946+
// Lowercase jquery is used because AMD module names are derived from
947+
// file names, and jQuery is normally delivered in a lowercase file name.
948+
if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
949+
define( "jquery", [], function () { return jQuery; } );
950+
}
951+
938952
return jQuery;
939953

940954
})();

test/data/testinit.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
var jQuery = this.jQuery || "jQuery", // For testing .noConflict()
22
$ = this.$ || "$",
33
originaljQuery = jQuery,
4-
original$ = $;
4+
original$ = $,
5+
amdDefined;
6+
7+
/**
8+
* Set up a mock AMD define function for testing AMD registration.
9+
*/
10+
function define(name, dependencies, callback) {
11+
amdDefined = callback();
12+
}
13+
14+
define.amd = {
15+
jQuery: true
16+
};
517

618
/**
719
* Returns an array of elements with the given IDs, eg.

test/unit/core.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ test("browser", function() {
225225
});
226226
}
227227

228+
test("amdModule", function() {
229+
expect(1);
230+
231+
equals( jQuery, amdDefined, "Make sure defined module matches jQuery" );
232+
});
233+
228234
test("noConflict", function() {
229235
expect(7);
230236

0 commit comments

Comments
 (0)