forked from jquery/jquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: use different ready code when Deferred is excluded
Fixes jquerygh-1778
- Loading branch information
Showing
3 changed files
with
131 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
define( [ | ||
"../core", | ||
"../var/document" | ||
], function( jQuery, document ) { | ||
|
||
var readyCallbacks = [], | ||
readyFiring = false, | ||
whenReady = function( fn ) { | ||
readyCallbacks.push( fn ); | ||
}; | ||
|
||
jQuery.fn.ready = function( fn ) { | ||
whenReady( fn ); | ||
return this; | ||
}; | ||
|
||
jQuery.extend( { | ||
|
||
// Is the DOM ready to be used? Set to true once it occurs. | ||
isReady: false, | ||
|
||
// A counter to track how many items to wait for before | ||
// the ready event fires. See #6781 | ||
readyWait: 1, | ||
|
||
// Hold (or release) the ready event | ||
holdReady: function( hold ) { | ||
if ( hold ) { | ||
jQuery.readyWait++; | ||
} else { | ||
jQuery.ready( true ); | ||
} | ||
}, | ||
|
||
ready: function( wait ) { | ||
|
||
// Abort if there are pending holds or we're already ready | ||
if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) { | ||
return; | ||
} | ||
|
||
// Remember that the DOM is ready | ||
jQuery.isReady = true; | ||
|
||
// If a normal DOM Ready event fired, decrement, and wait if need be | ||
if ( wait !== true && --jQuery.readyWait > 0 ) { | ||
return; | ||
} | ||
|
||
whenReady = function( fn ) { | ||
readyCallbacks.push( fn ); | ||
|
||
if ( !readyFiring ) { | ||
readyFiring = true; | ||
|
||
// Prevent errors from freezing future callback execution (gh-1823) | ||
try { | ||
while ( readyCallbacks.length ) { | ||
fn = readyCallbacks.shift(); | ||
if ( jQuery.isFunction( fn ) ) { | ||
|
||
// For backwards compatibility, | ||
// invoke synchronously and with document context | ||
fn.call( document, jQuery ); | ||
} | ||
} | ||
} finally { | ||
readyFiring = false; | ||
|
||
// If there was an error in a ready callback, | ||
// continue with the rest (gh-1823) | ||
if ( readyCallbacks.length ) { | ||
|
||
// Retry async to allow the error to propagate to console | ||
window.setTimeout( whenReady ); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
whenReady(); | ||
} | ||
} ); | ||
|
||
// Make jQuery.ready Promise consumable (gh-1778) | ||
jQuery.ready.then = jQuery.fn.ready; | ||
|
||
/** | ||
* The ready event handler and self cleanup method | ||
*/ | ||
function completed() { | ||
document.removeEventListener( "DOMContentLoaded", completed ); | ||
window.removeEventListener( "load", completed ); | ||
jQuery.ready(); | ||
} | ||
|
||
// Catch cases where $(document).ready() is called | ||
// after the browser event has already occurred. | ||
// Support: IE9-10 only | ||
// Older IE sometimes signals "interactive" too soon | ||
if ( document.readyState === "complete" || | ||
( document.readyState !== "loading" && !document.documentElement.doScroll ) ) { | ||
|
||
// Handle it asynchronously to allow scripts the opportunity to delay ready | ||
window.setTimeout( jQuery.ready ); | ||
|
||
} else { | ||
|
||
// Use the handy event callback | ||
document.addEventListener( "DOMContentLoaded", completed ); | ||
|
||
// A fallback to window.onload, that will always work | ||
window.addEventListener( "load", completed ); | ||
} | ||
|
||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters