From 5db70607d8e4f35a02f737884dcefa40653174ed Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Thu, 24 Dec 2015 02:39:07 +0100 Subject: [PATCH] mw.Upload.BookletLayout: Rewrite some code to use promise chaining Allegedly this makes it clearer. Follow-up to I0b2f53b91454f22e2001462397087da9e055b701. Change-Id: Ie3a6c20452d8102d44b4268f92c55bbabfc5cd68 --- ...i.ForeignStructuredUpload.BookletLayout.js | 33 +++++++++------ .../mediawiki.Upload.BookletLayout.js | 41 ++++++++++--------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js index 7331df9c1e..97d81fb478 100644 --- a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js +++ b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js @@ -39,21 +39,28 @@ * @inheritdoc */ mw.ForeignStructuredUpload.BookletLayout.prototype.initialize = function () { - var deferred = $.Deferred(); - mw.ForeignStructuredUpload.BookletLayout.parent.prototype.initialize.call( this ) - .done( function () { + var booklet = this; + return mw.ForeignStructuredUpload.BookletLayout.parent.prototype.initialize.call( this ).then( + function () { // Point the CategorySelector to the right wiki - this.upload.getApi().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 ); + return booklet.upload.getApi().then( + 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 + booklet.categoriesWidget.api = new mw.ForeignApi( api.apiUrl ); + } + return $.Deferred().resolve(); + }, + function () { + return $.Deferred().resolve(); } - }.bind( this ) ).always( function () { - deferred.resolve(); - } ); - }.bind( this ) ); - return deferred.promise(); + ); + }, + function () { + return $.Deferred().resolve(); + } + ); }; /** diff --git a/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js b/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js index 797106955e..9d67fe1edb 100644 --- a/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js +++ b/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js @@ -151,30 +151,33 @@ * @return {jQuery.Promise} Promise resolved when everything is initialized */ mw.Upload.BookletLayout.prototype.initialize = function () { - var - booklet = this, - deferred = $.Deferred(); + var booklet = this; this.clear(); this.upload = this.createUpload(); this.setPage( 'upload' ); - this.upload.getApi().done( function ( api ) { - // If the user can't upload anything, don't give them the option to. - api.getUserInfo().done( function ( userInfo ) { - if ( userInfo.rights.indexOf( 'upload' ) === -1 ) { - // TODO Use a better error message when not all logged-in users can upload - booklet.getPage( 'upload' ).$element.msg( 'api-error-mustbeloggedin' ); - } - } ).always( function () { - deferred.resolve(); - } ); - } ).fail( function ( errorMsg ) { - booklet.getPage( 'upload' ).$element.msg( errorMsg ); - deferred.resolve(); - } ); - - return deferred.promise(); + return this.upload.getApi().then( + function ( api ) { + // If the user can't upload anything, don't give them the option to. + return api.getUserInfo().then( + function ( userInfo ) { + if ( userInfo.rights.indexOf( 'upload' ) === -1 ) { + // TODO Use a better error message when not all logged-in users can upload + booklet.getPage( 'upload' ).$element.msg( 'api-error-mustbeloggedin' ); + } + return $.Deferred().resolve(); + }, + function () { + return $.Deferred().resolve(); + } + ); + }, + function ( errorMsg ) { + booklet.getPage( 'upload' ).$element.msg( errorMsg ); + return $.Deferred().resolve(); + } + ); }; /** -- 2.20.1