MDL-30811 javascript: Lazily load core dependencies

To prevent possible race conditions, we lazily load the templates, and ajax
dependencies.
This commit is contained in:
Andrew Nicols 2016-03-03 14:37:25 +08:00
parent ebdbc82b82
commit 11a67b211b
2 changed files with 22 additions and 19 deletions

View File

@ -1 +1 @@
define(["core/yui","jquery","theme_bootstrapbase/bootstrap","core/templates","core/ajax","core/log"],function(a,b,c,d,e,f){var g={types:{success:"core/notification_success",info:"core/notification_info",warning:"core/notification_warning",error:"core/notification_error"},fieldName:"user-notifications",fetchNotifications:function(){var a=e.call([{methodname:"core_fetch_notifications",args:{contextid:g.contextid}}]);a[0].done(g.addNotifications)},addNotifications:function(a){a||(a=[]),b.each(a,function(a,b){g.renderNotification(b.template,b.variables)})},setupTargetRegion:function(){var a=b("#"+g.fieldName);if(!a.length){var c=b("<span>").attr("id",g.fieldName);return a=b("#region-main"),a.length?a.prepend(c):(a=b('[role="main"]'),a.length?a.prepend(c):(a=b("body"),a.prepend(c)))}},addNotification:function(a){var c=g.types.error;return a=b.extend({closebutton:!0,announce:!0,type:"error"},a),a.template?(c=a.template,delete a.template):a.type&&("undefined"!=typeof g.types[a.type]&&(c=g.types[a.type]),delete a.type),g.renderNotification(c,a)},renderNotification:function(a,c){return"undefined"!=typeof c.message&&c.message?void d.render(a,c).done(function(a){b("#"+g.fieldName).prepend(a)}).fail(g.exception):void f.debug("Notification received without content. Skipping.")},alert:function(b,c,d){a.use("moodle-core-notification-alert",function(){var a=new M.core.alert({title:b,message:c,yesLabel:d});a.show()})},confirm:function(b,c,d,e,f){a.use("moodle-core-notification-confirm",function(){var a=new M.core.confirm({title:b,question:c,yesLabel:d,noLabel:e});a.on("complete-yes",function(){f()}),a.show()})},exception:function(b){b.backtrace&&(b.lineNumber=b.backtrace[0].line,b.fileName=b.backtrace[0].file,b.fileName="..."+b.fileName.substr(b.fileName.length-20),b.stack=b.debuginfo,b.name=b.errorcode),a.use("moodle-core-notification-exception",function(){var a=new M.core.exception(b);a.show()})}};return{init:function(a,c){g.contextid=a,g.setupTargetRegion(),b().alert(),g.addNotifications(c),g.fetchNotifications()},fetchNotifications:g.fetchNotifications,addNotification:g.addNotification,alert:g.alert,confirm:g.confirm,exception:g.exception}});
define(["core/yui","jquery","theme_bootstrapbase/bootstrap","core/log"],function(a,b,c,d){var e={types:{success:"core/notification_success",info:"core/notification_info",warning:"core/notification_warning",error:"core/notification_error"},fieldName:"user-notifications",fetchNotifications:function(){require(["core/ajax"],function(a){var b=a.call([{methodname:"core_fetch_notifications",args:{contextid:e.contextid}}]);b[0].done(e.addNotifications)})},addNotifications:function(a){a||(a=[]),b.each(a,function(a,b){e.renderNotification(b.template,b.variables)})},setupTargetRegion:function(){var a=b("#"+e.fieldName);if(!a.length){var c=b("<span>").attr("id",e.fieldName);return a=b("#region-main"),a.length?a.prepend(c):(a=b('[role="main"]'),a.length?a.prepend(c):(a=b("body"),a.prepend(c)))}},addNotification:function(a){var c=e.types.error;return a=b.extend({closebutton:!0,announce:!0,type:"error"},a),a.template?(c=a.template,delete a.template):a.type&&("undefined"!=typeof e.types[a.type]&&(c=e.types[a.type]),delete a.type),e.renderNotification(c,a)},renderNotification:function(a,c){return"undefined"!=typeof c.message&&c.message?void require(["core/templates"],function(d){d.render(a,c).done(function(a){b("#"+e.fieldName).prepend(a)}).fail(e.exception)}):void d.debug("Notification received without content. Skipping.")},alert:function(b,c,d){a.use("moodle-core-notification-alert",function(){var a=new M.core.alert({title:b,message:c,yesLabel:d});a.show()})},confirm:function(b,c,d,e,f){a.use("moodle-core-notification-confirm",function(){var a=new M.core.confirm({title:b,question:c,yesLabel:d,noLabel:e});a.on("complete-yes",function(){f()}),a.show()})},exception:function(b){b.backtrace&&(b.lineNumber=b.backtrace[0].line,b.fileName=b.backtrace[0].file,b.fileName="..."+b.fileName.substr(b.fileName.length-20),b.stack=b.debuginfo,b.name=b.errorcode),a.use("moodle-core-notification-exception",function(){var a=new M.core.exception(b);a.show()})}};return{init:function(a,c){e.contextid=a,e.setupTargetRegion(),b().alert(),e.addNotifications(c),e.fetchNotifications()},fetchNotifications:e.fetchNotifications,addNotification:e.addNotification,alert:e.alert,confirm:e.confirm,exception:e.exception}});

View File

@ -26,8 +26,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
define(['core/yui', 'jquery', 'theme_bootstrapbase/bootstrap', 'core/templates', 'core/ajax', 'core/log'],
function(Y, $, bootstrap, templates, ajax, log) {
define(['core/yui', 'jquery', 'theme_bootstrapbase/bootstrap', 'core/log'],
function(Y, $, bootstrap, log) {
var notificationModule = {
types: {
'success': 'core/notification_success',
@ -39,17 +39,18 @@ function(Y, $, bootstrap, templates, ajax, log) {
fieldName: 'user-notifications',
fetchNotifications: function() {
var promises = ajax.call([{
methodname: 'core_fetch_notifications',
args: {
contextid: notificationModule.contextid
}
}]);
promises[0]
.done(notificationModule.addNotifications)
;
require(['core/ajax'], function(ajax) {
var promises = ajax.call([{
methodname: 'core_fetch_notifications',
args: {
contextid: notificationModule.contextid
}
}]);
promises[0]
.done(notificationModule.addNotifications)
;
});
},
addNotifications: function(notifications) {
@ -111,12 +112,14 @@ function(Y, $, bootstrap, templates, ajax, log) {
log.debug('Notification received without content. Skipping.');
return;
}
templates.render(template, variables)
.done(function(html) {
$('#' + notificationModule.fieldName).prepend(html);
})
.fail(notificationModule.exception)
;
require(['core/templates'], function(templates) {
templates.render(template, variables)
.done(function(html) {
$('#' + notificationModule.fieldName).prepend(html);
})
.fail(notificationModule.exception)
;
});
},
alert: function(title, message, yesLabel) {