web staff : env supports ad-hoc loaders; more todo
authorBill Erickson <berick@esilibrary.com>
Mon, 28 Oct 2013 13:10:32 +0000 (09:10 -0400)
committerBill Erickson <berick@esilibrary.com>
Mon, 28 Oct 2013 13:10:32 +0000 (09:10 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/web/js/ui/default/staff/services/env.js

index cd8cf8c..ca55f08 100644 (file)
@@ -6,6 +6,23 @@
  * returns, a pending-calls counter is decremented.  Once
  * it reaches zero, the promise returned by load() / 
  * loadAll() is resolved.
+ *
+ * App-supplied generic load commands are pushed onto the 
+ * service.loaders array.  Each item in the array is a 
+ * function which returns a promise.  Note that loaders in this
+ * array cannot have any dependencies on other startup loaders,
+ * since there is no guarantee which will complete first.
+ *
+ * egEnv.loaders.push(function() {
+ *  return egNet.request(...)
+ *  .then(function(stuff) { console.log('stuff!') });
+ *
+ * Data requets that have requirements loaded by startup
+ * should run after startup directly in the application.
+ *
+ * TODO: marry egStartup and egEnv?
+ * TODO: support a post-org loader so that loaders can be
+ *      added which rely on the org tree?
  */
 
 angular.module('egCoreMod')
@@ -18,10 +35,11 @@ function($cacheFactory) {
 
 // env fetcher
 .factory('egEnv', 
-    ['$q', 'egEnvCache', 'egNet', 'egAuth', 'egPCRUD',
+['$q', 'egEnvCache', 'egNet', 'egAuth', 'egPCRUD',
 function($q, egEnvCache, egNet, egAuth, egPCRUD) { 
 
     var service = {
+        loaders : [],
         get : function(class_) {
             return egEnvCache.get(class_);
         }
@@ -34,11 +52,14 @@ function($q, egEnvCache, egNet, egAuth, egPCRUD) {
 
     /* returns a promise, loads all of the specified classes */
     service.load = function(classes) {
-        if (!classes) classes = Object.keys(this.loaders);
+        if (!classes) classes = Object.keys(this.classLoaders);
         this.deferred = $q.defer();
-        this.in_flight = classes.length;
+        this.in_flight = classes.length + this.loaders.length;
         angular.forEach(classes, function(cls) {
-            service.loaders[cls]().then(function(){service.onload()});
+            service.classLoaders[cls]().then(function(){service.onload()});
+        });
+        angular.forEach(this.loaders, function(loader) {
+            loader().then(function(){service.onload()});
         });
         return this.deferred.promise;
     };
@@ -68,7 +89,7 @@ function($q, egEnvCache, egNet, egAuth, egPCRUD) {
     /* Classes (by hint) to load, their loading routines,
      * and their result mungers */
 
-    service.loaders = {
+    service.classLoaders = {
 
         aou : function() {
             return egPCRUD.search('aou', {parent_ou : null}, 
@@ -88,7 +109,7 @@ function($q, egEnvCache, egNet, egAuth, egPCRUD) {
         }
 
         // org unit settings, blah, blah
-    }
+    };
 
     return service;
 }]);