@@ -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
818840window . 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} ) ( ) ;
0 commit comments