mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
Revert "Core: remove custom ready event"
This reverts commit b35bea14a9
.
This commit is contained in:
parent
a57975fb74
commit
86f0c5af9c
@ -1,6 +1,7 @@
|
|||||||
define( [
|
define( [
|
||||||
"../core",
|
"../core",
|
||||||
"../var/document",
|
"../var/document",
|
||||||
|
"../core/init",
|
||||||
"../deferred"
|
"../deferred"
|
||||||
], function( jQuery, document ) {
|
], function( jQuery, document ) {
|
||||||
|
|
||||||
@ -51,6 +52,12 @@ jQuery.extend( {
|
|||||||
|
|
||||||
// If there are functions bound, to execute
|
// If there are functions bound, to execute
|
||||||
readyList.resolveWith( document, [ jQuery ] );
|
readyList.resolveWith( document, [ jQuery ] );
|
||||||
|
|
||||||
|
// Trigger any bound ready events
|
||||||
|
if ( jQuery.fn.triggerHandler ) {
|
||||||
|
jQuery( document ).triggerHandler( "ready" );
|
||||||
|
jQuery( document ).off( "ready" );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
QUnit.module( "ready" );
|
QUnit.module( "ready" );
|
||||||
|
|
||||||
( function() {
|
(function(){
|
||||||
var notYetReady, noEarlyExecution,
|
var notYetReady, noEarlyExecution,
|
||||||
order = [],
|
order = [],
|
||||||
args = {};
|
args = {};
|
||||||
@ -16,7 +16,6 @@ QUnit.module( "ready" );
|
|||||||
|
|
||||||
// Create an event handler.
|
// Create an event handler.
|
||||||
function makeHandler( testId ) {
|
function makeHandler( testId ) {
|
||||||
|
|
||||||
// When returned function is executed, push testId onto `order` array
|
// When returned function is executed, push testId onto `order` array
|
||||||
// to ensure execution order. Also, store event handler arg to ensure
|
// to ensure execution order. Also, store event handler arg to ensure
|
||||||
// the correct arg is being passed into the event handler.
|
// the correct arg is being passed into the event handler.
|
||||||
@ -27,44 +26,48 @@ QUnit.module( "ready" );
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Bind to the ready event in every possible way.
|
// Bind to the ready event in every possible way.
|
||||||
jQuery( makeHandler( "a" ) );
|
jQuery(makeHandler("a"));
|
||||||
jQuery( document ).ready( makeHandler( "b" ) );
|
jQuery(document).ready(makeHandler("b"));
|
||||||
|
jQuery(document).on("ready.readytest", makeHandler("c"));
|
||||||
|
|
||||||
// Do it twice, just to be sure.
|
// Do it twice, just to be sure.
|
||||||
jQuery( makeHandler( "c" ) );
|
jQuery(makeHandler("d"));
|
||||||
jQuery( document ).ready( makeHandler( "d" ) );
|
jQuery(document).ready(makeHandler("e"));
|
||||||
|
jQuery(document).on("ready.readytest", makeHandler("f"));
|
||||||
|
|
||||||
noEarlyExecution = order.length === 0;
|
noEarlyExecution = order.length === 0;
|
||||||
|
|
||||||
// This assumes that QUnit tests are run on DOM ready!
|
// This assumes that QUnit tests are run on DOM ready!
|
||||||
QUnit.test( "jQuery ready", function( assert ) {
|
QUnit.test("jQuery ready", function( assert ) {
|
||||||
assert.expect( 8 );
|
expect(10);
|
||||||
|
|
||||||
assert.ok( noEarlyExecution,
|
ok(noEarlyExecution, "Handlers bound to DOM ready should not execute before DOM ready");
|
||||||
"Handlers bound to DOM ready should not execute before DOM ready" );
|
|
||||||
|
|
||||||
// Ensure execution order.
|
// Ensure execution order.
|
||||||
assert.deepEqual( order, [ "a", "b", "c", "d" ],
|
deepEqual(order, ["a", "b", "d", "e", "c", "f"], "Bound DOM ready handlers should execute in on-order, but those bound with jQuery(document).on( 'ready', fn ) will always execute last");
|
||||||
"Bound DOM ready handlers should execute in on-order" );
|
|
||||||
|
|
||||||
// Ensure handler argument is correct.
|
// Ensure handler argument is correct.
|
||||||
assert.equal( args.a, jQuery,
|
equal(args["a"], jQuery, "Argument passed to fn in jQuery( fn ) should be jQuery");
|
||||||
"Argument passed to fn in jQuery( fn ) should be jQuery" );
|
equal(args["b"], jQuery, "Argument passed to fn in jQuery(document).ready( fn ) should be jQuery");
|
||||||
assert.equal( args.b, jQuery,
|
ok(args["c"] instanceof jQuery.Event, "Argument passed to fn in jQuery(document).on( 'ready', fn ) should be an event object");
|
||||||
"Argument passed to fn in jQuery(document).ready( fn ) should be jQuery" );
|
|
||||||
|
|
||||||
order = [];
|
order = [];
|
||||||
|
|
||||||
// Now that the ready event has fired, again bind to the ready event
|
// Now that the ready event has fired, again bind to the ready event
|
||||||
// in every possible way. These event handlers should execute immediately.
|
// in every possible way. These event handlers should execute immediately.
|
||||||
jQuery( makeHandler( "g" ) );
|
jQuery(makeHandler("g"));
|
||||||
assert.equal( order.pop(), "g", "Event handler should execute immediately" );
|
equal(order.pop(), "g", "Event handler should execute immediately");
|
||||||
assert.equal( args.g, jQuery, "Argument passed to fn in jQuery( fn ) should be jQuery" );
|
equal(args["g"], jQuery, "Argument passed to fn in jQuery( fn ) should be jQuery");
|
||||||
|
|
||||||
jQuery( document ).ready( makeHandler( "h" ) );
|
jQuery(document).ready(makeHandler("h"));
|
||||||
assert.equal( order.pop(), "h", "Event handler should execute immediately" );
|
equal(order.pop(), "h", "Event handler should execute immediately");
|
||||||
assert.equal( args.h, jQuery,
|
equal(args["h"], jQuery, "Argument passed to fn in jQuery(document).ready( fn ) should be jQuery");
|
||||||
"Argument passed to fn in jQuery(document).ready( fn ) should be jQuery" );
|
|
||||||
} );
|
jQuery(document).on("ready.readytest", makeHandler("never"));
|
||||||
|
equal(order.length, 0, "Event handler should never execute since DOM ready has already passed");
|
||||||
|
|
||||||
|
// Cleanup.
|
||||||
|
jQuery(document).off("ready.readytest");
|
||||||
|
});
|
||||||
|
|
||||||
} )();
|
} )();
|
||||||
|
Loading…
Reference in New Issue
Block a user