MDL-50783 Ajax: Configure how to call a webservice through db/service.php
[moodle.git] / lib / amd / src / ajax.js
index 2966cb4..6b497c3 100644 (file)
@@ -104,9 +104,13 @@ define(['jquery', 'core/config'], function($, config) {
          *                   can be attached to the promises returned by this function.
          * @param {Boolean} async Optional, defaults to true.
          *                  If false - this function will not return until the promises are resolved.
+         * @param {Boolean} loginrequired Optional, defaults to true.
+         *                  If false - this function will call the faster nologin ajax script - but
+         *                  will fail unless all functions have been marked as 'loginrequired' => false
+         *                  in services.php
          * @return {Promise[]} Array of promises that will be resolved when the ajax call returns.
          */
-        call: function(requests, async) {
+        call: function(requests, async, loginrequired) {
             var ajaxRequestData = [],
                 i,
                 promises = [];
@@ -144,15 +148,20 @@ define(['jquery', 'core/config'], function($, config) {
                 async: async
             };
 
+            var script = config.wwwroot + '/lib/ajax/service.php?sesskey=' + config.sesskey;
+            if (!loginrequired) {
+                script = config.wwwroot + '/lib/ajax/service-nologin.php?sesskey=' + config.sesskey;
+            }
+
             // Jquery deprecated done and fail with async=false so we need to do this 2 ways.
             if (async) {
-                $.ajax(config.wwwroot + '/lib/ajax/service.php?sesskey=' + config.sesskey, settings)
+                $.ajax(script, settings)
                     .done(requestSuccess)
                     .fail(requestFail);
             } else {
                 settings.success = requestSuccess;
                 settings.error = requestFail;
-                $.ajax(config.wwwroot + '/lib/ajax/service.php?sesskey=' + config.sesskey, settings);
+                $.ajax(script, settings);
             }
 
             return promises;