webstaff: work log: fix retrieving of entries
authorGalen Charlton <gmc@esilibrary.com>
Wed, 4 May 2016 21:08:04 +0000 (17:08 -0400)
committerGalen Charlton <gmc@esilibrary.com>
Wed, 4 May 2016 21:08:04 +0000 (17:08 -0400)
* add support for paging through worklog entries
* don't prematurely resolve the promise that is
  feeding rows to the grid

FIXME: because of the async, the entries are
not guaranteed to be sorted correctly.

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Open-ILS/web/js/ui/default/staff/admin/workstation/log.js

index 2fdd575..39dbc72 100644 (file)
@@ -72,7 +72,7 @@ function($scope , $q , $routeParams , $window , $timeout , egCore , egGridDataPr
     }
 
     function fetch_hold(deferred,entry) {
-        egCore.pcrud.search('ahr',
+        return egCore.pcrud.search('ahr',
             { 'id' : entry.data.hold_id }, {
                 'flesh' : 2,
                 'flesh_fields' : {
@@ -80,7 +80,6 @@ function($scope , $q , $routeParams , $window , $timeout , egCore , egGridDataPr
                 },
             }
         ).then(
-            deferred.resolve, null, 
             function(hold) {
                 entry.patron_id = hold.usr().id();
                 entry.user = hold.usr().family_name();
@@ -93,10 +92,9 @@ function($scope , $q , $routeParams , $window , $timeout , egCore , egGridDataPr
     }
 
     function fetch_patron(deferred,entry) {
-        egCore.pcrud.search('au',
+        return egCore.pcrud.search('au',
             { 'id' : entry.data.patron_id }, {}
         ).then(
-            deferred.resolve, null,
             function(usr) {
                 entry.user = usr.family_name();
                 deferred.notify(entry);
@@ -109,24 +107,28 @@ function($scope , $q , $routeParams , $window , $timeout , egCore , egGridDataPr
         console.log(log_entries);
         var deferred = $q.defer();
 
-        $timeout( function() {
-            log_entries.work_log.forEach(
-                function(el,idx) {
-                    el.id = idx;
-                    if (el.action == 'requested_hold') {
-                        fetch_hold(deferred,el);
-                    } else if (el.action == 'registered_patron') {
-                        fetch_patron(deferred,el);
-                    } else if (el.action == 'edited_patron') {
-                        fetch_patron(deferred,el);
-                    } else if (el.action == 'paid_bill') {
-                        fetch_patron(deferred,el);
-                    } else {
-                        deferred.notify(el);
-                    }
+        var promises = [];
+        var entries = count ?
+                      log_entries.work_log.slice(offset, offset + count) :
+                      log_entries.work_log;
+        entries.forEach(
+            function(el,idx) {
+                el.id = idx;
+                if (el.action == 'requested_hold') {
+                    promises.push(fetch_hold(deferred,el));
+                } else if (el.action == 'registered_patron') {
+                    promises.push(fetch_patron(deferred,el));
+                } else if (el.action == 'edited_patron') {
+                    promises.push(fetch_patron(deferred,el));
+                } else if (el.action == 'paid_bill') {
+                    promises.push(fetch_patron(deferred,el));
+                } else {
+                    promises.push($timeout(function() { deferred.notify(el) }));
                 }
-            );
-        });
+            }
+        );
+        $q.all(promises).then(deferred.resolve);
+
         return deferred.promise;
     }
 
@@ -135,24 +137,28 @@ function($scope , $q , $routeParams , $window , $timeout , egCore , egGridDataPr
         console.log(log_entries);
         var deferred = $q.defer();
 
-        $timeout( function() {
-            log_entries.patron_log.forEach(
-                function(el,idx) {
-                    el.id = idx;
-                    if (el.action == 'requested_hold') {
-                        fetch_hold(deferred,el);
-                    } else if (el.action == 'registered_patron') {
-                        fetch_patron(deferred,el);
-                    } else if (el.action == 'edited_patron') {
-                        fetch_patron(deferred,el);
-                    } else if (el.action == 'paid_bill') {
-                        fetch_patron(deferred,el);
-                    } else {
-                        deferred.notify(el);
-                    }
+        var promises = [];
+        var entries = count ?
+                      log_entries.patron_log.slice(offset, offset + count) :
+                      log_entries.patron_log;
+        log_entries.patron_log.forEach(
+            function(el,idx) {
+                el.id = idx;
+                if (el.action == 'requested_hold') {
+                    promises.push(fetch_hold(deferred,el));
+                } else if (el.action == 'registered_patron') {
+                    promises.push(fetch_patron(deferred,el));
+                } else if (el.action == 'edited_patron') {
+                    promises.push(fetch_patron(deferred,el));
+                } else if (el.action == 'paid_bill') {
+                    promises.push(fetch_patron(deferred,el));
+                } else {
+                    promises.push($timeout(function() { deferred.notify(el) }));
                 }
-            );
-        });
+            }
+        );
+        $q.all(promises).then(deferred.resolve);
+
         return deferred.promise;
     }