Skip to content

Commit 0178972

Browse files
committed
Allows jQuery to integrate with the require() provided by RequireJS, if it is available. Exposes jQuery as a module that can be required and makes sure jQuery ready callbacks are not fired if scripts are still being loaded by require(). Includes unit tests.
1 parent 6a0942c commit 0178972

File tree

19 files changed

+1782
-22
lines changed

19 files changed

+1782
-22
lines changed

src/core.js

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ var jQuery = function( selector, context ) {
4040

4141
// For matching the engine and version of the browser
4242
browserMatch,
43-
43+
44+
// Should require integration be used?
45+
useRequire = !!( typeof require !== "undefined" && require.def ),
46+
47+
// require could have page load logic built in so remember its function
48+
requireReadyCallback = useRequire && require.callReady,
49+
4450
// Has the ready events already been bound?
4551
readyBound = false,
4652

@@ -236,15 +242,24 @@ jQuery.fn = jQuery.prototype = {
236242
// Attach the listeners
237243
jQuery.bindReady();
238244

239-
// If the DOM is already ready
240-
if ( jQuery.isReady ) {
245+
// If the DOM is already ready, and if require is in use
246+
// all scripts have finished loading
247+
if ( jQuery.isReady && ( !useRequire || require.s.isDone ) ) {
241248
// Execute the function immediately
242249
fn.call( document, jQuery );
243250

244251
// Otherwise, remember the function for later
245-
} else if ( readyList ) {
246-
// Add the function to the wait list
247-
readyList.push( fn );
252+
} else {
253+
// readyList could have been cleared for the initial
254+
// page load, but if scripts are loaded via require after
255+
// page load, then need to allow for other ready callbacks
256+
// to be registered that indicate those scripts after page
257+
// load have finished loading.
258+
if ( !readyList ) {
259+
readyList = [];
260+
}
261+
// Add the function to the wait list
262+
readyList.push( fn );
248263
}
249264

250265
return this;
@@ -371,24 +386,31 @@ jQuery.extend({
371386
// Remember that the DOM is ready
372387
jQuery.isReady = true;
373388

374-
// If there are functions bound, to execute
375-
if ( readyList ) {
376-
// Execute all of them
377-
var fn, i = 0;
378-
while ( (fn = readyList[ i++ ]) ) {
379-
fn.call( document, jQuery );
380-
}
381-
382-
// Reset the list of functions
383-
readyList = null;
384-
}
385-
386-
// Trigger any bound ready events
387-
if ( jQuery.fn.triggerHandler ) {
388-
jQuery( document ).triggerHandler( "ready" );
389-
}
389+
jQuery.callReady();
390390
}
391391
},
392+
393+
// Calls ready callbacks, can be triggered by require script loading.
394+
callReady: function() {
395+
if ( jQuery.isReady && (!useRequire || require.s.isDone) ) {
396+
// If there are functions bound, to execute
397+
if ( readyList ) {
398+
// Execute all of them
399+
var fn, i = 0;
400+
while ( (fn = readyList[ i++ ]) ) {
401+
fn.call( document, jQuery );
402+
}
403+
404+
// Reset the list of functions
405+
readyList = null;
406+
}
407+
408+
// Trigger any bound ready events
409+
if ( jQuery.fn.triggerHandler ) {
410+
jQuery( document ).triggerHandler( "ready" );
411+
}
412+
}
413+
},
392414

393415
bindReady: function() {
394416
if ( readyBound ) {
@@ -817,4 +839,19 @@ function doScrollCheck() {
817839
// Expose jQuery to the global object
818840
window.jQuery = window.$ = jQuery;
819841

842+
// Integrate with require
843+
if (useRequire) {
844+
// Register script load completion callback
845+
require.callReady = function() {
846+
// If require has its own ready callback functionality, call it.
847+
if ( requireReadyCallback ) {
848+
requireReadyCallback();
849+
}
850+
jQuery.callReady();
851+
};
852+
853+
// Register jQuery as a module
854+
require.def("jquery", function() { return jQuery; });
855+
}
856+
820857
})();

test/require/data/dimple.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require.def("dimple",
2+
{
3+
color: "dimple-blue"
4+
}
5+
);

test/require/data/dos.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require.def("dos",
2+
["tres"],
3+
function(tres) {
4+
return {
5+
name: "dos",
6+
doSomething: function() {
7+
return {
8+
tresName: tres.name
9+
};
10+
}
11+
};
12+
}
13+
);

test/require/data/epsilon.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
/*global require: false */
3+
require.def("epsilon",
4+
{
5+
name: "epsilon"
6+
}
7+
);

test/require/data/func.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require.def("func",
2+
function () {
3+
return function () {
4+
return "You called a function";
5+
}
6+
}
7+
);

test/require/data/funcFour.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require.def("funcFour",
2+
["require", "funcThree"],
3+
function (require) {
4+
var four = function (arg) {
5+
return "FOUR called with " + arg;
6+
};
7+
8+
four.suffix = function () {
9+
return require("funcThree").suffix();
10+
};
11+
12+
return four;
13+
}
14+
);

test/require/data/funcOne.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require.def("funcOne",
2+
["require", "funcTwo"],
3+
function (require) {
4+
var one = function (name) {
5+
this.name = name;
6+
};
7+
8+
one.prototype.getName = function () {
9+
var inst = new (require("funcTwo"))("-NESTED");
10+
return this.name + inst.name;
11+
};
12+
13+
return one;
14+
}
15+
);

test/require/data/funcThree.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require.def("funcThree",
2+
["funcFour"],
3+
function (four) {
4+
var three = function (arg) {
5+
return arg + "-" + require("funcFour").suffix();
6+
};
7+
8+
three.suffix = function () {
9+
return "THREE_SUFFIX";
10+
};
11+
12+
return three;
13+
}
14+
);

test/require/data/funcTwo.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require.def("funcTwo",
2+
["funcOne"],
3+
function () {
4+
var two = function (name) {
5+
this.name = name;
6+
this.one = new (require("funcOne"))("ONE");
7+
};
8+
9+
two.prototype.oneName = function () {
10+
return this.one.getName();
11+
};
12+
13+
return two;
14+
}
15+
);

test/require/data/layer1.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//Example layer file.
2+
3+
"use strict";
4+
/*global require: false */
5+
6+
require.pause();
7+
8+
require.def("alpha",
9+
["beta", "gamma"],
10+
function (beta, gamma) {
11+
return {
12+
name: "alpha",
13+
betaName: beta.name
14+
};
15+
}
16+
);
17+
18+
require.def("beta",
19+
["gamma"],
20+
function (gamma) {
21+
return {
22+
name: "beta",
23+
gammaName: gamma.name
24+
};
25+
}
26+
);
27+
28+
require.def("gamma",
29+
["epsilon"],
30+
function (epsilon) {
31+
return {
32+
name: "gamma",
33+
epsilonName: epsilon.name
34+
};
35+
}
36+
);
37+
38+
require.resume();

0 commit comments

Comments
 (0)