From 7f43fccd4d53349a418fc82c30f4dfaa53d14070 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Mon, 8 Sep 2014 00:47:58 +0200 Subject: [PATCH] mw.ViewPageTarget.init: Reduce Deferred clutter in getTarget() * mw.loader.using returns a promise already. * Use .then() instead of manually forwarding failures. * Cache .promise() result. Change-Id: I3d0a90051c389c02dd5714020b2a1fb872790a18 --- .../targets/ve.init.mw.ViewPageTarget.init.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.init.js b/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.init.js index 76633f5c8c..f5ac2e74a4 100644 --- a/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.init.js +++ b/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.init.js @@ -18,7 +18,7 @@ */ ( function () { var conf, tabMessages, uri, pageExists, viewUri, veEditUri, isViewPage, - init, support, getTargetDeferred, enable, userPrefEnabled, + init, support, targetPromise, enable, userPrefEnabled, plugins = []; /** @@ -26,11 +26,9 @@ * @returns {jQuery.Promise} */ function getTarget() { - var loadTargetDeferred; - if ( !getTargetDeferred ) { - getTargetDeferred = $.Deferred(); - loadTargetDeferred = $.Deferred() - .done( function () { + if ( !targetPromise ) { + targetPromise = mw.loader.using( 'ext.visualEditor.viewPageTarget' ) + .then( function () { var target = new ve.init.mw.ViewPageTarget(); // Tee tracked events to MediaWiki firehose, if available (1.23+). @@ -46,13 +44,10 @@ // Add plugins target.addPlugins( plugins ); - getTargetDeferred.resolve( target ); - } ) - .fail( getTargetDeferred.reject ); - - mw.loader.using( 'ext.visualEditor.viewPageTarget', loadTargetDeferred.resolve, loadTargetDeferred.reject ); + return target; + } ); } - return getTargetDeferred.promise(); + return targetPromise; } conf = mw.config.get( 'wgVisualEditorConfig' );