cache org setting values for ws_ou
authorBill Erickson <berick@esilibrary.com>
Tue, 13 May 2014 14:37:42 +0000 (10:37 -0400)
committerBill Erickson <berick@esilibrary.com>
Tue, 13 May 2014 14:37:42 +0000 (10:37 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/web/js/ui/default/staff/services/org.js

index b07c41b..8ff7137 100644 (file)
@@ -11,6 +11,10 @@ function($q,  egEnv,  egAuth,  egNet) {
 
     var service = {};
 
+    // org unit settings cache.
+    // This allows the caller to avoid local caches
+    service.cachedSettings = {};
+
     service.get = function(node_or_id) {
         if (typeof node_or_id == 'object')
             return node_or_id;
@@ -67,6 +71,26 @@ function($q,  egEnv,  egAuth,  egNet) {
     service.settings = function(names, ou_id) {
         var deferred = $q.defer();
         ou_id = ou_id || egAuth.user().ws_ou();
+        var here = (ou_id == egAuth.user().ws_ou());
+
+        // allow non-array
+        if (!angular.isArray(names)) names = [names];
+        
+        if (here) { 
+            // only cache org settings retrieved for the current 
+            // workstation org unit.
+            var newNames = [];
+            angular.forEach(names, function(name) {
+                if (!angular.isDefined(service.cachedSettings[name]))
+                    newNames.push(name)
+            });
+
+            // only retrieve uncached values
+            names = newNames;
+            if (names.length == 0)
+                return $q.when(service.cachedSettings);
+        }
+
         egNet.request(
             'open-ils.actor',
             'open-ils.actor.ou_setting.ancestor_default.batch',
@@ -74,11 +98,14 @@ function($q,  egEnv,  egAuth,  egNet) {
         ).then(function(blob) {
             var settings = {};
             angular.forEach(blob, function(val, key) {
-                // val is either null or a structure containing 
-                // the value
-                if (val) { settings[key] = val.value }
+                // val is either null or a structure containing the value
+                settings[key] = val ? val.value : null;
+                if (here) service.cachedSettings[key] = settings[key];
             });
-            deferred.resolve(settings);
+
+            // resolve with cached settings if 'here', since 'settings'
+            // will only contain settings we had to retrieve
+            deferred.resolve(here ? service.cachedSettings : settings);
         });
         return deferred.promise;
     }