MDL-57139 mod_lti: ensure promise best practices

This commit is contained in:
Dan Poltawski 2017-01-10 11:07:34 +00:00
parent 7efdac5fc3
commit 1fea12b0eb
2 changed files with 43 additions and 46 deletions

View File

@ -46,42 +46,41 @@ define(
* @param {object} postData The data to be sent for the content item selection request. * @param {object} postData The data to be sent for the content item selection request.
*/ */
init: function(url, postData) { init: function(url, postData) {
var dialogueTitle = ''; var context = {
url: url,
postData: postData
};
var bodyPromise = templates.render('mod_lti/contentitem', context);
if (dialogue) {
// Set dialogue body.
dialogue.setBody(bodyPromise);
// Display the dialogue.
dialogue.show();
return;
}
str.get_string('selectcontent', 'lti').then(function(title) { str.get_string('selectcontent', 'lti').then(function(title) {
dialogueTitle = title; return ModalFactory.create({
var context = { title: title,
url: url, body: bodyPromise,
postData: postData 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('');
var body = templates.render('mod_lti/contentitem', context); // Fetch notifications.
if (dialogue) { notification.fetchNotifications();
// 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;
// Display the dialogue. // Display the dialogue.
dialogue.show(); modal.show();
return;
// On hide handler. }).catch(notification.exception);
modal.getRoot().on(ModalEvents.hidden, function() {
// Empty modal contents when it's hidden.
modal.setBody('');
// Fetch notifications.
notification.fetchNotifications();
});
});
}
});
} }
}; };

View File

@ -477,21 +477,19 @@ define(['jquery', 'core/ajax', 'core/notification', 'core/templates', 'mod_lti/t
state: toolType.constants.state.configured state: toolType.constants.state.configured
}); });
promise.done(function(toolTypeData) { promise.then(function(toolTypeData) {
stopLoading(element); stopLoading(element);
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];
var announcePromise = announceSuccess(element); templates.replaceNode(element, html, js);
var renderPromise = templates.render('mod_lti/tool_card', toolTypeData); return;
}).catch(function() {
$.when(renderPromise, announcePromise).then(function(renderResult) {
var html = renderResult[0];
var js = renderResult[1];
templates.replaceNode(element, html, js);
});
});
promise.fail(function() {
stopLoading(element); stopLoading(element);
announceFailure(element); announceFailure(element);
}); });