From b36d8ace6d61c83267ea308d190dec704549bb41 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Fri, 2 Dec 2011 21:47:46 +0000 Subject: [PATCH] Followup r104671: fix regression in jquery.delayedBind() due to change in param processing for jquery's bind() Problem was per CR comment https://www.mediawiki.org/wiki/Special:Code/MediaWiki/104671#c26836 -- parameter count check changed in upstream jquery.bind(), causing the way we passed params to break if the optional 'data' parameter was left out. Added QUnit test cases for jquery.delayedBind() with and without the optional 'data' parameter. Undoes r104671 since the issue it was working around is fixed. --- resources/jquery/jquery.collapsibleTabs.js | 2 +- resources/jquery/jquery.delayedBind.js | 5 +++ tests/qunit/index.html | 2 + .../jquery/jquery.delayedBind.test.js | 41 +++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/qunit/suites/resources/jquery/jquery.delayedBind.test.js diff --git a/resources/jquery/jquery.collapsibleTabs.js b/resources/jquery/jquery.collapsibleTabs.js index df7a935332..1784f86ab5 100644 --- a/resources/jquery/jquery.collapsibleTabs.js +++ b/resources/jquery/jquery.collapsibleTabs.js @@ -24,7 +24,7 @@ // if we haven't already bound our resize hanlder, bind it now if( !$.collapsibleTabs.boundEvent ) { $( window ) - .delayedBind( '500', 'resize', null, function( ) { $.collapsibleTabs.handleResize(); } ); + .delayedBind( '500', 'resize', function( ) { $.collapsibleTabs.handleResize(); } ); } // call our resize handler to setup the page $.collapsibleTabs.handleResize(); diff --git a/resources/jquery/jquery.delayedBind.js b/resources/jquery/jquery.delayedBind.js index 6d97299623..d84ee267a4 100644 --- a/resources/jquery/jquery.delayedBind.js +++ b/resources/jquery/jquery.delayedBind.js @@ -19,6 +19,11 @@ $.fn.extend( { * @param callback Function to call */ delayedBind: function( timeout, event, data, callback ) { + if ( arguments.length == 3 ) { + // Shift optional parameter down + callback = data; + data = undefined; + } var encEvent = encodeEvent( event ); return this.each( function() { var that = this; diff --git a/tests/qunit/index.html b/tests/qunit/index.html index 918718691d..ac8087241e 100644 --- a/tests/qunit/index.html +++ b/tests/qunit/index.html @@ -62,6 +62,7 @@ + @@ -91,6 +92,7 @@ + diff --git a/tests/qunit/suites/resources/jquery/jquery.delayedBind.test.js b/tests/qunit/suites/resources/jquery/jquery.delayedBind.test.js new file mode 100644 index 0000000000..8688f12e0b --- /dev/null +++ b/tests/qunit/suites/resources/jquery/jquery.delayedBind.test.js @@ -0,0 +1,41 @@ +test('jquery.delayedBind with data option', function() { + var $fixture = $('
').appendTo('body'), + data = { magic: "beeswax" }, + delay = 50; + + $fixture.delayedBind(delay, 'testevent', data, function(event) { + start(); // continue! + ok(true, 'testevent fired'); + ok(event.data === data, 'data is passed through delayedBind'); + }); + + expect(2); + stop(); // async! + + // We'll trigger it thrice, but it should only happen once. + $fixture.trigger('testevent', {}); + $fixture.trigger('testevent', {}); + $fixture.trigger('testevent', {}); + $fixture.trigger('testevent', {}); +}); + +test('jquery.delayedBind without data option', function() { + var $fixture = $('
').appendTo('body'), + data = { magic: "beeswax" }, + delay = 50; + + $fixture.delayedBind(delay, 'testevent', function(event) { + start(); // continue! + ok(true, 'testevent fired'); + }); + + expect(1); + stop(); // async! + + // We'll trigger it thrice, but it should only happen once. + $fixture.trigger('testevent', {}); + $fixture.trigger('testevent', {}); + $fixture.trigger('testevent', {}); + $fixture.trigger('testevent', {}); +}); + -- 2.20.1