From: Oleg Gaidarenko Date: Wed, 11 Nov 2015 16:50:26 +0000 (+0300) Subject: Revert "Core: remove custom ready event" X-Git-Tag: 2.2.0~57 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1b52d383ce8a23113d48c26c21a3400429d9de1e;p=jquery.git Revert "Core: remove custom ready event" This reverts commit c252c5fac2a25869524c2eba2e93d4230e6af25c. --- diff --git a/src/core/ready.js b/src/core/ready.js index 73c3d706a..7d93e6768 100644 --- a/src/core/ready.js +++ b/src/core/ready.js @@ -1,6 +1,7 @@ define( [ "../core", "../var/document", + "../core/init", "../deferred" ], function( jQuery, document ) { @@ -51,6 +52,12 @@ jQuery.extend( { // If there are functions bound, to execute readyList.resolveWith( document, [ jQuery ] ); + + // Trigger any bound ready events + if ( jQuery.fn.triggerHandler ) { + jQuery( document ).triggerHandler( "ready" ); + jQuery( document ).off( "ready" ); + } } } ); diff --git a/test/unit/ready.js b/test/unit/ready.js index 6272dbdd4..337568c8f 100644 --- a/test/unit/ready.js +++ b/test/unit/ready.js @@ -29,29 +29,34 @@ QUnit.module( "ready" ); // Bind to the ready event in every possible way. jQuery( makeHandler( "a" ) ); jQuery( document ).ready( makeHandler( "b" ) ); + jQuery( document ).on( "ready.readytest", makeHandler( "c" ) ); // Do it twice, just to be sure. - jQuery( makeHandler( "c" ) ); - jQuery( document ).ready( makeHandler( "d" ) ); + jQuery( makeHandler( "d" ) ); + jQuery( document ).ready( makeHandler( "e" ) ); + jQuery( document ).on( "ready.readytest", makeHandler( "f" ) ); noEarlyExecution = order.length === 0; // This assumes that QUnit tests are run on DOM ready! QUnit.test( "jQuery ready", function( assert ) { - assert.expect( 8 ); + assert.expect( 10 ); - assert.ok( noEarlyExecution, - "Handlers bound to DOM ready should not execute before DOM ready" ); + assert.ok( noEarlyExecution, "Handlers bound to DOM ready should not execute before DOM ready" ); // Ensure execution order. - assert.deepEqual( order, [ "a", "b", "c", "d" ], - "Bound DOM ready handlers should execute in on-order" ); + assert.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" ); // Ensure handler argument is correct. - assert.equal( args.a, jQuery, + assert.equal( args[ "a" ], jQuery, "Argument passed to fn in jQuery( fn ) should be jQuery" ); - assert.equal( args.b, jQuery, + assert.equal( args[ "b" ], jQuery, "Argument passed to fn in jQuery(document).ready( fn ) should be jQuery" ); + assert.ok( args[ "c" ] instanceof jQuery.Event, + "Argument passed to fn in jQuery(document).on( 'ready', fn )" + + " should be an event object" ); order = []; @@ -59,12 +64,20 @@ QUnit.module( "ready" ); // in every possible way. These event handlers should execute immediately. jQuery( makeHandler( "g" ) ); assert.equal( order.pop(), "g", "Event handler should execute immediately" ); - assert.equal( args.g, jQuery, "Argument passed to fn in jQuery( fn ) should be jQuery" ); + assert.equal( args[ "g" ], jQuery, + "Argument passed to fn in jQuery( fn ) should be jQuery" ); jQuery( document ).ready( makeHandler( "h" ) ); assert.equal( order.pop(), "h", "Event handler should execute immediately" ); - assert.equal( args.h, jQuery, + assert.equal( args[ "h" ], jQuery, "Argument passed to fn in jQuery(document).ready( fn ) should be jQuery" ); + + jQuery( document ).on( "ready.readytest", makeHandler( "never" ) ); + assert.equal( order.length, 0, + "Event handler should never execute since DOM ready has already passed" ); + + // Cleanup. + jQuery( document ).off( "ready.readytest" ); } ); } )();