MDL-59784 core: Modal factory should listen before fetching templates
authorAndrew Nicols <andrew@nicols.co.uk>
Wed, 16 Aug 2017 04:32:46 +0000 (12:32 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Fri, 18 Aug 2017 14:51:48 +0000 (22:51 +0800)
lib/amd/build/modal_factory.min.js
lib/amd/src/modal_factory.js

index efa71d9..02e0604 100644 (file)
Binary files a/lib/amd/build/modal_factory.min.js and b/lib/amd/build/modal_factory.min.js differ
index 678bad8..f9db208 100644 (file)
@@ -55,10 +55,10 @@ define(['jquery', 'core/modal_events', 'core/modal_registry', 'core/modal',
      * is closed.
      *
      * @method setUpTrigger
      * is closed.
      *
      * @method setUpTrigger
-     * @param {object} modal The modal instance
+     * @param {Promise} modalPromise The modal instance
      * @param {object} triggerElement The jQuery element to open the modal
      */
      * @param {object} triggerElement The jQuery element to open the modal
      */
-    var setUpTrigger = function(modal, triggerElement) {
+    var setUpTrigger = function(modalPromise, triggerElement) {
         if (typeof triggerElement != 'undefined') {
             if (Array.isArray(triggerElement)) {
                 var selector = triggerElement[1];
         if (typeof triggerElement != 'undefined') {
             if (Array.isArray(triggerElement)) {
                 var selector = triggerElement[1];
@@ -66,19 +66,31 @@ define(['jquery', 'core/modal_events', 'core/modal_registry', 'core/modal',
 
                 CustomEvents.define(triggerElement, [CustomEvents.events.activate]);
                 triggerElement.on(CustomEvents.events.activate, selector, function(e, data) {
 
                 CustomEvents.define(triggerElement, [CustomEvents.events.activate]);
                 triggerElement.on(CustomEvents.events.activate, selector, function(e, data) {
-                    modal.show();
+                    modalPromise.then(function(modal) {
+                        modal.show();
+
+                        return modal;
+                    });
                     data.originalEvent.preventDefault();
                 });
             } else {
                 CustomEvents.define(triggerElement, [CustomEvents.events.activate]);
                 triggerElement.on(CustomEvents.events.activate, function(e, data) {
                     data.originalEvent.preventDefault();
                 });
             } else {
                 CustomEvents.define(triggerElement, [CustomEvents.events.activate]);
                 triggerElement.on(CustomEvents.events.activate, function(e, data) {
-                    modal.show();
+                    modalPromise.then(function(modal) {
+                        modal.show();
+
+                        return modal;
+                    });
                     data.originalEvent.preventDefault();
                 });
             }
 
                     data.originalEvent.preventDefault();
                 });
             }
 
-            modal.getRoot().on(ModalEvents.hidden, function() {
-                triggerElement.focus();
+            modalPromise.then(function(modal) {
+                modal.getRoot().on(ModalEvents.hidden, function() {
+                    triggerElement.focus();
+                });
+
+                return modal;
             });
         }
     };
             });
         }
     };
@@ -93,11 +105,10 @@ define(['jquery', 'core/modal_events', 'core/modal_registry', 'core/modal',
      * @param {object} triggerElement The trigger HTML jQuery object
      * @return {object} Modal instance
      */
      * @param {object} triggerElement The trigger HTML jQuery object
      * @return {object} Modal instance
      */
-    var createFromElement = function(registryConf, modalElement, triggerElement) {
+    var createFromElement = function(registryConf, modalElement) {
         modalElement = $(modalElement);
         var module = registryConf.module;
         var modal = new module(modalElement);
         modalElement = $(modalElement);
         var module = registryConf.module;
         var modal = new module(modalElement);
-        setUpTrigger(modal, triggerElement);
 
         return modal;
     };
 
         return modal;
     };
@@ -115,12 +126,16 @@ define(['jquery', 'core/modal_events', 'core/modal_registry', 'core/modal',
     var createFromType = function(registryConf, templateContext, triggerElement) {
         var templateName = registryConf.template;
 
     var createFromType = function(registryConf, templateContext, triggerElement) {
         var templateName = registryConf.template;
 
-        return Templates.render(templateName, templateContext)
+        var modalPromise = Templates.render(templateName, templateContext)
             .then(function(html) {
                 var modalElement = $(html);
             .then(function(html) {
                 var modalElement = $(html);
-                return createFromElement(registryConf, modalElement, triggerElement);
+                return createFromElement(registryConf, modalElement);
             })
             .fail(Notification.exception);
             })
             .fail(Notification.exception);
+
+        setUpTrigger(modalPromise, triggerElement);
+
+        return modalPromise;
     };
 
     /**
     };
 
     /**