MDL-57139 mod_lti: ensure promise best practices
authorDan Poltawski <dan@moodle.com>
Tue, 10 Jan 2017 11:07:34 +0000 (11:07 +0000)
committerDan Poltawski <dan@moodle.com>
Thu, 1 Jun 2017 09:27:59 +0000 (10:27 +0100)
mod/lti/amd/src/contentitem.js
mod/lti/amd/src/tool_card_controller.js

index 42e9808..a6dad92 100644 (file)
@@ -46,42 +46,41 @@ define(
              * @param {object} postData The data to be sent for the content item selection request.
              */
             init: function(url, postData) {
-                var dialogueTitle = '';
-                str.get_string('selectcontent', 'lti').then(function(title) {
-                    dialogueTitle = title;
-                    var context = {
-                        url: url,
-                        postData: postData
-                    };
+                var context = {
+                    url: url,
+                    postData: postData
+                };
+                var bodyPromise = templates.render('mod_lti/contentitem', context);
 
-                    var body = templates.render('mod_lti/contentitem', context);
-                    if (dialogue) {
-                        // Set dialogue body.
-                        dialogue.setBody(body);
-                        // Display the dialogue.
-                        dialogue.show();
-                    } else {
-                        ModalFactory.create({
-                            title: dialogueTitle,
-                            body: body,
-                            large: true
-                        }).done(function(modal) {
-                            dialogue = modal;
+                if (dialogue) {
+                    // Set dialogue body.
+                    dialogue.setBody(bodyPromise);
+                    // Display the dialogue.
+                    dialogue.show();
+                    return;
+                }
 
-                            // Display the dialogue.
-                            dialogue.show();
+                str.get_string('selectcontent', 'lti').then(function(title) {
+                    return ModalFactory.create({
+                        title: title,
+                        body: bodyPromise,
+                        large: true
+                    });
+                }).then(function(modal) {
+                    dialogue = modal;
+                    // On hide handler.
+                    modal.getRoot().on(ModalEvents.hidden, function() {
+                        // Empty modal contents when it's hidden.
+                        modal.setBody('');
 
-                            // On hide handler.
-                            modal.getRoot().on(ModalEvents.hidden, function() {
-                                // Empty modal contents when it's hidden.
-                                modal.setBody('');
+                        // Fetch notifications.
+                        notification.fetchNotifications();
+                    });
 
-                                // Fetch notifications.
-                                notification.fetchNotifications();
-                            });
-                        });
-                    }
-                });
+                    // Display the dialogue.
+                    modal.show();
+                    return;
+                }).catch(notification.exception);
             }
         };
 
index 99042db..4050f72 100644 (file)
@@ -477,21 +477,19 @@ define(['jquery', 'core/ajax', 'core/notification', 'core/templates', 'mod_lti/t
             state: toolType.constants.state.configured
         });
 
-        promise.done(function(toolTypeData) {
+        promise.then(function(toolTypeData) {
             stopLoading(element);
-
-            var announcePromise = announceSuccess(element);
-            var renderPromise = templates.render('mod_lti/tool_card', toolTypeData);
-
-            $.when(renderPromise, announcePromise).then(function(renderResult) {
-                var html = renderResult[0];
-                var js = renderResult[1];
-
-                templates.replaceNode(element, html, js);
-            });
-        });
-
-        promise.fail(function() {
+            announceSuccess(element);
+            return toolTypeData;
+        }).then(function(toolTypeData) {
+            return templates.render('mod_lti/tool_card', toolTypeData);
+        }).then(function(renderResult) {
+            var html = renderResult[0];
+            var js = renderResult[1];
+
+            templates.replaceNode(element, html, js);
+            return;
+        }).catch(function() {
             stopLoading(element);
             announceFailure(element);
         });