MDL-30811 javascript: Lazily load core dependencies
authorAndrew Nicols <andrew@nicols.co.uk>
Thu, 3 Mar 2016 06:37:25 +0000 (14:37 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Thu, 3 Mar 2016 06:47:07 +0000 (14:47 +0800)
To prevent possible race conditions, we lazily load the templates, and ajax
dependencies.

lib/amd/build/notification.min.js
lib/amd/src/notification.js

index 7df55c2..d6b1ee7 100644 (file)
Binary files a/lib/amd/build/notification.min.js and b/lib/amd/build/notification.min.js differ
index 845a2e6..bb8cdb4 100644 (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) {