MDL-57139 myoverview: Use promise best practices
authorDan Poltawski <dan@moodle.com>
Thu, 1 Jun 2017 11:37:15 +0000 (12:37 +0100)
committerDan Poltawski <dan@moodle.com>
Thu, 1 Jun 2017 13:54:38 +0000 (14:54 +0100)
Simplified promise
* Always return
* Make use of promise chaining features to simplfy flow

blocks/myoverview/amd/src/event_list.js

index 17e888f..f1d273e 100644 (file)
@@ -350,34 +350,37 @@ define(['jquery', 'core/notification', 'core/templates',
 
         // Request data from the server.
         return promise.then(function(result) {
-            return result.events;
-        }).then(function(calendarEvents) {
-            if (!calendarEvents.length || (calendarEvents.length < limit)) {
-                // We have no more events so mark the list as done.
+            if (!result.events.length) {
+                // No events, nothing to do.
                 setLoadedAll(root);
+                return 0;
             }
 
-            if (calendarEvents.length) {
-                // Remember the last id we've seen.
-                root.attr('data-last-id', calendarEvents[calendarEvents.length - 1].id);
-
-                // Render the events.
-                return render(root, calendarEvents).then(function(renderCount) {
-                    updateContentVisibility(root, calendarEvents.length);
-
-                    if (renderCount < calendarEvents.length) {
-                        // if the number of events that was rendered is less than
-                        // the number we sent for rendering we can assume that there
-                        // are no groups to add them in. Since the ordering of the
-                        // events is guaranteed it means that any future requests will
-                        // also yield events that can't be rendered, so let's not bother
-                        // sending any more requests.
-                        setLoadedAll(root);
-                    }
-                });
-            } else {
-                updateContentVisibility(root, calendarEvents.length);
+            var calendarEvents = result.events;
+
+            // Remember the last id we've seen.
+            root.attr('data-last-id', calendarEvents[calendarEvents.length - 1].id);
+
+            if (calendarEvents.length <= limit) {
+                // No more events to load, disable loading button.
+                setLoadedAll(root);
             }
+
+            // Render the events.
+            return render(root, calendarEvents).then(function(renderCount) {
+                if (renderCount < calendarEvents.length) {
+                    // if the number of events that was rendered is less than
+                    // the number we sent for rendering we can assume that there
+                    // are no groups to add them in. Since the ordering of the
+                    // events is guaranteed it means that any future requests will
+                    // also yield events that can't be rendered, so let's not bother
+                    // sending any more requests.
+                    setLoadedAll(root);
+                }
+                return calendarEvents.length;
+            });
+        }).then(function(eventCount) {
+            return updateContentVisibility(root, eventCount);
         }).fail(
             Notification.exception
         ).always(function() {