MDL-63094 tool_policy: Fix race condition in modal display
authorAndrew Nicols <andrew@nicols.co.uk>
Wed, 8 Aug 2018 01:58:22 +0000 (09:58 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 22 Aug 2018 02:23:30 +0000 (10:23 +0800)
The way in which the modal was displayed meant that there were no
pending JS items, whilst the modal was rendered, causing random behat
fails.

This JS has been restructured to create the Modal and pass it a set of
Promises for both the title, and body.

admin/tool/policy/amd/build/policyactions.min.js
admin/tool/policy/amd/src/policyactions.js

index 9a87001..2e54773 100644 (file)
Binary files a/admin/tool/policy/amd/build/policyactions.min.js and b/admin/tool/policy/amd/build/policyactions.min.js differ
index ae70f48..b3de54a 100644 (file)
@@ -65,43 +65,55 @@ function($, Ajax, Notification, ModalFactory, ModalEvents) {
                 args: params
             };
 
+            var modalTitle = $.Deferred();
+            var modalBody = $.Deferred();
+
+            var modal = ModalFactory.create({
+                title: modalTitle,
+                body: modalBody,
+                large: true
+            })
+            .then(function(modal) {
+                // Handle hidden event.
+                modal.getRoot().on(ModalEvents.hidden, function() {
+                    // Destroy when hidden.
+                    modal.destroy();
+                });
+
+                return modal;
+            })
+            .then(function(modal) {
+                modal.show();
+
+                return modal;
+            })
+            .catch(Notification.exception);
+
+            // Make the request now that the modal is configured.
             var promises = Ajax.call([request]);
-            var modalTitle = '';
-            var modalType = ModalFactory.types.DEFAULT;
             $.when(promises[0]).then(function(data) {
                 if (data.result.policy) {
-                    modalTitle = data.result.policy.name;
-                    return data.result.policy.content;
+                    modalTitle.resolve(data.result.policy.name);
+                    modalBody.resolve(data.result.policy.content);
+
+                    return data;
+                } else {
+                    throw new Error(data.warnings[0].message);
                 }
-                // Fail.
-                Notification.addNotification({
-                    message: data.warnings[0].message,
+            }).catch(function(message) {
+                modal.then(function(modal) {
+                    modal.hide();
+                    modal.destroy();
+
+                    return modal;
+                })
+                .catch(Notification.exception);
+
+                return Notification.addNotification({
+                    message: message,
                     type: 'error'
                 });
-                return false;
-
-            }).then(function(html) {
-                if (html != false) {
-                    return ModalFactory.create({
-                        title: modalTitle,
-                        body: html,
-                        type: modalType,
-                        large: true
-                    }).then(function(modal) {
-                        // Handle hidden event.
-                        modal.getRoot().on(ModalEvents.hidden, function() {
-                            // Destroy when hidden.
-                            modal.destroy();
-                        });
-
-                        return modal;
-                    });
-                }
-                return false;
-            }).done(function(modal) {
-                // Show the modal.
-                modal.show();
-            }).fail(Notification.exception);
+            });
         });
 
     };