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
This commit is contained in:
Timo Tijhof 2014-09-08 00:47:58 +02:00
parent 69750e4b41
commit 7f43fccd4d

View file

@ -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' );