MDL-57139 core/fragment: simplify promises and always return
authorDan Poltawski <dan@moodle.com>
Tue, 10 Jan 2017 17:29:31 +0000 (17:29 +0000)
committerDan Poltawski <dan@moodle.com>
Thu, 1 Jun 2017 09:27:59 +0000 (10:27 +0100)
We can remove the DIY promise handling because the ajax request can just
return a promise itself.

lib/amd/src/fragment.js

index 28627fc..0a29f68 100644 (file)
@@ -45,10 +45,7 @@ define(['jquery', 'core/ajax'], function($, ajax) {
             });
         }
 
-        // Ajax stuff.
-        var deferred = $.Deferred();
-
-        var promises = ajax.call([{
+        return ajax.call([{
             methodname: 'core_get_fragment',
             args: {
                 component: component,
@@ -56,14 +53,7 @@ define(['jquery', 'core/ajax'], function($, ajax) {
                 contextid: contextid,
                 args: formattedparams
             }
-        }], false);
-
-        promises[0].done(function(data) {
-            deferred.resolve(data);
-        }).fail(function(ex) {
-            deferred.reject(ex);
-        });
-        return deferred.promise();
+        }])[0];
     };
 
     return /** @alias module:core/fragment */{
@@ -81,7 +71,7 @@ define(['jquery', 'core/ajax'], function($, ajax) {
          */
         loadFragment: function(component, callback, contextid, params) {
             var promise = $.Deferred();
-            $.when(loadFragment(component, callback, contextid, params)).then(function(data) {
+            loadFragment(component, callback, contextid, params).then(function(data) {
                 var jsNodes = $(data.javascript);
                 var allScript = '';
                 jsNodes.each(function(index, scriptNode) {
@@ -111,6 +101,7 @@ define(['jquery', 'core/ajax'], function($, ajax) {
                     }
                 });
                 promise.resolve(data.html, allScript);
+                return;
             }).fail(function(ex) {
                 promise.reject(ex);
             });