MDL-63094 tool_policy: Fix race condition in modal display

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.
This commit is contained in:
Andrew Nicols
2018-08-22 10:27:58 +08:00
parent 94409c69b1
commit fed3dbe83e
2 changed files with 44 additions and 32 deletions
+1 -1
View File
@@ -1 +1 @@
define(["jquery","core/ajax","core/notification","core/modal_factory","core/modal_events"],function(a,b,c,d,e){var f={VIEW_POLICY:'[data-action="view"]'},g=function(){this.registerEvents()};return g.prototype.registerEvents=function(){a(f.VIEW_POLICY).click(function(f){f.preventDefault();var g=a(this).data("versionid"),h=a(this).data("behalfid"),i={versionid:g,behalfid:h},j={methodname:"tool_policy_get_policy_version",args:i},k=b.call([j]),l="",m=d.types.DEFAULT;a.when(k[0]).then(function(a){return a.result.policy?(l=a.result.policy.name,a.result.policy.content):(c.addNotification({message:a.warnings[0].message,type:"error"}),!1)}).then(function(a){return 0!=a&&d.create({title:l,body:a,type:m,large:!0}).then(function(a){return a.getRoot().on(e.hidden,function(){a.destroy()}),a})}).done(function(a){a.show()}).fail(c.exception)})},{init:function(){return new g}}});
define(["jquery","core/ajax","core/notification","core/modal_factory","core/modal_events"],function(a,b,c,d,e){var f={VIEW_POLICY:'[data-action="view"]'},g=function(){this.registerEvents()};return g.prototype.registerEvents=function(){a(f.VIEW_POLICY).click(function(f){f.preventDefault();var g=a(this).data("versionid"),h=a(this).data("behalfid"),i={versionid:g,behalfid:h},j={methodname:"tool_policy_get_policy_version",args:i},k=a.Deferred(),l=a.Deferred(),m=d.create({title:k,body:l,large:!0}).then(function(a){return a.getRoot().on(e.hidden,function(){a.destroy()}),a}).then(function(a){return a.show(),a})["catch"](c.exception),n=b.call([j]);a.when(n[0]).then(function(a){if(a.result.policy)return k.resolve(a.result.policy.name),l.resolve(a.result.policy.content),a;throw new Error(a.warnings[0].message)})["catch"](function(a){return m.then(function(a){return a.hide(),a.destroy(),a})["catch"](c.exception),c.addNotification({message:a,type:"error"})})})},{init:function(){return new g}}});
+43 -31
View 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);
});
});
};