Skip to content

Commit a5715e6

Browse files
committed
Add support for restricting a version of jQuery within a context to a specific version, supports http://bugs.jquery.com/ticket/7102.
1 parent 87479aa commit a5715e6

6 files changed

Lines changed: 137 additions & 21 deletions

File tree

require.js

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ var require, define;
3939
isDone = false,
4040
useInteractive = false,
4141
req, cfg = {}, currentlyAddingScript, s, head, baseElement, scripts, script,
42-
src, subPath, mainScript, dataMain, i, scrollIntervalId, setReadyState, ctx;
42+
src, subPath, mainScript, dataMain, i, scrollIntervalId, setReadyState, ctx,
43+
jQueryCheck;
4344

4445
function isFunction(it) {
4546
return ostring.call(it) === "[object Function]";
@@ -585,19 +586,25 @@ var require, define;
585586

586587
if (fullName) {
587588
//If module already defined for context, or already loaded,
588-
//then leave.
589-
if (fullName in defined || loaded[fullName] === true) {
589+
//then leave. Also leave if jQuery is registering but it does
590+
//not match the desired version number in the config.
591+
if (fullName in defined || loaded[fullName] === true ||
592+
(fullName === "jquery" && config.jQuery &&
593+
config.jQuery !== callback().fn.jquery)) {
590594
return;
591595
}
592596

593597
//Set specified/loaded here for modules that are also loaded
594598
//as part of a layer, where onScriptLoad is not fired
595599
//for those cases. Do this after the inline define and
596600
//dependency tracing is done.
597-
//Also check if auto-registry of jQuery needs to be skipped.
598601
specified[fullName] = true;
599602
loaded[fullName] = true;
600-
context.jQueryDef = (fullName === "jquery");
603+
604+
//If module is jQuery set up delaying its dom ready listeners.
605+
if (fullName === "jquery") {
606+
jQueryCheck(callback());
607+
}
601608
}
602609

603610
//Add the dependencies to the deps field, and register for callbacks
@@ -682,26 +689,37 @@ var require, define;
682689
* name of the global. If a jQuery is tracked for this context, then go
683690
* ahead and register it as a module too, if not already in process.
684691
*/
685-
function jQueryCheck(jqCandidate) {
692+
jQueryCheck = function (jqCandidate) {
686693
if (!context.jQuery) {
687694
var $ = jqCandidate || (typeof jQuery !== "undefined" ? jQuery : null);
688-
if ($ && "readyWait" in $) {
689-
context.jQuery = $;
690-
691-
//Manually create a "jquery" module entry if not one already
692-
//or in process.
693-
callDefMain(["jquery", [], function () {
694-
return jQuery;
695-
}]);
696-
697-
//Increment jQuery readyWait if ncecessary.
698-
if (context.scriptCount) {
699-
$.readyWait += 1;
700-
context.jQueryIncremented = true;
695+
696+
if ($) {
697+
//If a specific version of jQuery is wanted, make sure to only
698+
//use this jQuery if it matches.
699+
if (config.jQuery && $.fn.jquery !== config.jQuery) {
700+
return;
701+
}
702+
703+
if ("readyWait" in $) {
704+
context.jQuery = $;
705+
706+
//Manually create a "jquery" module entry if not one already
707+
//or in process. Note this could trigger an attempt at
708+
//a second jQuery registration, but does no harm since
709+
//the first one wins, and it is the same value anyway.
710+
callDefMain(["jquery", [], function () {
711+
return jQuery;
712+
}]);
713+
714+
//Increment jQuery readyWait if ncecessary.
715+
if (context.scriptCount) {
716+
$.readyWait += 1;
717+
context.jQueryIncremented = true;
718+
}
701719
}
702720
}
703721
}
704-
}
722+
};
705723

706724
function forceExec(manager, traced) {
707725
if (manager.isDone) {
@@ -1548,7 +1566,8 @@ var require, define;
15481566

15491567
define.amd = {
15501568
multiversion: true,
1551-
plugins: true
1569+
plugins: true,
1570+
jQuery: true
15521571
};
15531572

15541573
/**

tasks.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Next release
1515
- IE checkLoaded recursion? https://github.com/jrburke/requirejs/issues/82
1616
- Document in README the directory structure.
1717
- Support jQuery holdReady
18+
- Document config.jQuery support
1819

1920
- "libs/static.lib.02", //kaaahhhhnnnn!!!
2021
- optimizer as a separate project?

tests/all.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ doh.registerUrl("uniques", "../uniques/uniques.html");
3232
doh.registerUrl("multiversion", "../multiversion.html", 10000);
3333
doh.registerUrl("jquery", "../jquery/jquery.html");
3434
doh.registerUrl("jqueryPriority", "../jquery/jqueryPriority.html");
35+
doh.registerUrl("jqueryVersion", "../jquery/jqueryVersion.html");
36+
doh.registerUrl("jqueryVersion2", "../jquery/jqueryVersion2.html");
3537

3638
//Next three tests fail in PS3
3739
doh.registerUrl("jqueryDynamic", "../jquery/jqueryDynamic.html");

tests/jquery/jqueryVersion.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>jQuery Version Test</title>
5+
<script src="../../require.js"></script>
6+
<script type="text/javascript" src="../doh/runner.js"></script>
7+
<script type="text/javascript" src="../doh/_browserRunner.js"></script>
8+
<script>
9+
require({
10+
baseUrl: "./scripts/",
11+
paths: {
12+
jquery: "jquery-1.5.2"
13+
},
14+
jQuery: "1.5.2"
15+
},
16+
["require", "jquery-1.4.4"],
17+
function(require) {
18+
require(["require", "jquery"], function (require) {
19+
doh.register(
20+
"jqueryVersion",
21+
[
22+
function jqueryVersion(t){
23+
t.is("1.5.2", require('jquery').fn.jquery);
24+
}
25+
]
26+
);
27+
doh.run();
28+
});
29+
}
30+
);
31+
</script>
32+
</head>
33+
<body>
34+
<h1>jQuery Version Test</h1>
35+
<p>Tests loading of two different jQuery versions but only using the
36+
second one since it was the one specified in the require config.</p>
37+
<p>Check console for output.</p>
38+
</body>
39+
</html>

tests/jquery/jqueryVersion2.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>jQuery Version Test</title>
5+
<script src="../../require.js"></script>
6+
<script type="text/javascript" src="../doh/runner.js"></script>
7+
<script type="text/javascript" src="../doh/_browserRunner.js"></script>
8+
<script>
9+
require({
10+
baseUrl: "./scripts/",
11+
paths: {
12+
jquery: "jquery-1.5.2"
13+
},
14+
jQuery: "1.4.4"
15+
},
16+
["require", "jquery-1.4.4"],
17+
function(require) {
18+
require(["require", "jquery"], function (require) {
19+
doh.register(
20+
"jqueryVersion2",
21+
[
22+
function jqueryVersion2(t){
23+
t.is("1.4.4", require('jquery').fn.jquery);
24+
}
25+
]
26+
);
27+
doh.run();
28+
});
29+
}
30+
);
31+
</script>
32+
</head>
33+
<body>
34+
<h1>jQuery Version Test</h1>
35+
<p>Tests loading of two different jQuery versions but only using the
36+
second one since it was the one specified in the require config.</p>
37+
<p>Check console for output.</p>
38+
</body>
39+
</html>

tests/jquery/scripts/jquery-1.5.2.js

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)