Merge "mw.Upload.BookletLayout: Rewrite some code to use promise chaining"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 25 Jan 2016 12:07:41 +0000 (12:07 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 25 Jan 2016 12:07:41 +0000 (12:07 +0000)
resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js
resources/src/mediawiki/mediawiki.Upload.BookletLayout.js

index 7331df9..97d81fb 100644 (file)
         * @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();
+                       }
+               );
        };
 
        /**
index 4038228..c8d64b9 100644 (file)
         * @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();
+                       }
+               );
        };
 
        /**