From d2a3225f483e275eefcddc193d55181914eb0542 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Wed, 28 Oct 2015 22:28:51 +0100 Subject: [PATCH] mediawiki.Upload.BookletLayout: Allow for asynchronous initialization It happens to be convenient, since it turned out that we need to fire a few API requests to correctly render some bits of the UI. Follow-up to 86dedeea7f7cae5452acf5162672f34c32b593fd. This makes the code less hacky and more reliable. Change-Id: I65a92e3915a6844056fb7b3a2758a168eb12e6b8 --- ...i.ForeignStructuredUpload.BookletLayout.js | 23 +++++++++++-------- .../mediawiki.Upload.BookletLayout.js | 3 +++ .../src/mediawiki/mediawiki.Upload.Dialog.js | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js index 01917f8a4d..717e2ecfab 100644 --- a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js +++ b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js @@ -39,15 +39,20 @@ * @inheritdoc */ mw.ForeignStructuredUpload.BookletLayout.prototype.initialize = function () { - mw.ForeignStructuredUpload.BookletLayout.parent.prototype.initialize.call( this ); - // Point the CategorySelector to the right wiki as soon as we know what the right wiki is - this.upload.apiPromise.done( function ( api ) { - // If this is a ForeignApi, it will have a apiUrl, otherwise we don't need to do anything - if ( api.apiUrl ) { - // Can't reuse the same object, CategorySelector calls #abort on its mw.Api instance - this.categoriesWidget.api = new mw.ForeignApi( api.apiUrl ); - } - }.bind( this ) ); + var deferred = $.Deferred(); + mw.ForeignStructuredUpload.BookletLayout.parent.prototype.initialize.call( this ) + .done( function () { + // Point the CategorySelector to the right wiki + this.upload.apiPromise.done( function ( api ) { + // If this is a ForeignApi, it will have a apiUrl, otherwise we don't need to do anything + if ( api.apiUrl ) { + // Can't reuse the same object, CategorySelector calls #abort on its mw.Api instance + this.categoriesWidget.api = new mw.ForeignApi( api.apiUrl ); + } + deferred.resolve(); + }.bind( this ) ); + }.bind( this ) ); + return deferred.promise(); }; /** diff --git a/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js b/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js index dd199ceff7..c616829d89 100644 --- a/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js +++ b/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js @@ -147,11 +147,14 @@ /** * Initialize for a new upload + * + * @return {jQuery.Promise} Promise resolved when everything is initialized */ mw.Upload.BookletLayout.prototype.initialize = function () { this.clear(); this.upload = this.createUpload(); this.setPage( 'upload' ); + return $.Deferred().resolve().promise(); }; /** diff --git a/resources/src/mediawiki/mediawiki.Upload.Dialog.js b/resources/src/mediawiki/mediawiki.Upload.Dialog.js index 220a3fe3bb..53afca812d 100644 --- a/resources/src/mediawiki/mediawiki.Upload.Dialog.js +++ b/resources/src/mediawiki/mediawiki.Upload.Dialog.js @@ -174,7 +174,7 @@ mw.Upload.Dialog.prototype.getSetupProcess = function ( data ) { return mw.Upload.Dialog.parent.prototype.getSetupProcess.call( this, data ) .next( function () { - this.uploadBooklet.initialize(); + return this.uploadBooklet.initialize(); }, this ); }; -- 2.20.1