From: Timo Tijhof Date: Fri, 4 Apr 2014 01:35:33 +0000 (-0700) Subject: mw.hook: Make hook.fire actually chainable X-Git-Tag: 1.31.0-rc.0~16375^2 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=9674a17c0f3b5ef50ea572bdcbd4b277aa3d956d;p=lhc%2Fweb%2Fwiklou.git mw.hook: Make hook.fire actually chainable It was being called without being bound to the mw.hook instance, as such the (detachable) callbackList.fireWith method (which returns its given 'this') returned the callbackList instance instead of the mw.hook object literal. The tests weren't catching it because it was just checking one can call .fire() without it crashing, but it was in fact calling list.fire instead of hook.fire (which unlike #add and #remove are not compatible, and we might add new methods later on). Change-Id: If3d4dfbed494e7ff9f32514e539cfa089aea18ac --- diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index 885fd3da65..57f85d8516 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -2394,7 +2394,7 @@ var mw = ( function ( $, undefined ) { * @chainable */ fire: function () { - return list.fireWith( null, slice.call( arguments ) ); + return list.fireWith.call( this, null, slice.call( arguments ) ); } }; }; diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js index f5091f967b..41b0cb7035 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js @@ -851,7 +851,7 @@ } ); - QUnit.test( 'mw.hook', 10, function ( assert ) { + QUnit.test( 'mw.hook', 12, function ( assert ) { var hook, add, fire, chars, callback; mw.hook( 'test.hook.unfired' ).add( function () { @@ -869,9 +869,10 @@ } ); mw.hook( 'test.hook.data' ).fire( 'example', ['two'] ); - mw.hook( 'test.hook.chainable' ).add( function () { - assert.ok( true, 'Chainable' ); - } ).fire(); + hook = mw.hook( 'test.hook.chainable' ); + assert.strictEqual( hook.add(), hook, 'hook.add is chainable' ); + assert.strictEqual( hook.remove(), hook, 'hook.remove is chainable' ); + assert.strictEqual( hook.fire(), hook, 'hook.fire is chainable' ); hook = mw.hook( 'test.hook.detach' ); add = hook.add;