mediawiki.Upload.BookletLayout: Allow for asynchronous initialization
authorBartosz Dziewoński <matma.rex@gmail.com>
Wed, 28 Oct 2015 21:28:51 +0000 (22:28 +0100)
committerBartosz Dziewoński <matma.rex@gmail.com>
Wed, 28 Oct 2015 21:59:14 +0000 (22:59 +0100)
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

resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js
resources/src/mediawiki/mediawiki.Upload.BookletLayout.js
resources/src/mediawiki/mediawiki.Upload.Dialog.js

index 01917f8..717e2ec 100644 (file)
         * @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();
        };
 
        /**
index dd199ce..c616829 100644 (file)
 
        /**
         * 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();
        };
 
        /**
index 220a3fe..53afca8 100644 (file)
        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 );
        };