crmApp, crmUtil - Tighten scope of crmApp. Move services to crmUtil. Avoid cyclic...
authorTim Otten <totten@civicrm.org>
Sat, 3 Jan 2015 17:46:00 +0000 (09:46 -0800)
committerTim Otten <totten@civicrm.org>
Sat, 3 Jan 2015 17:51:25 +0000 (09:51 -0800)
All services, routes, controllers, and directives should live in specific
Angular modules.  When it comes time to display a complete web page, these
modules are aggregated to form crmApp.  crmApp is a thin piece of glue whose
sole purpose is aggregation.

js/angular-crm-util.js
js/angular-crmApp.js
js/angular-crmCaseType.js
js/angular-crmMailing.js
tests/karma/modules.js
tests/karma/unit/crmMailingSpec.js

index b67659cf4612dcf3bbbe218073c56ee7d37b7438..d27875a2b8ae96cef34bb718d3c7ee9b4127119f 100644 (file)
@@ -2,6 +2,38 @@
 (function (angular, $, _) {
   angular.module('crmUtil', []);
 
+  angular.module('crmUtil').factory('crmApi', function($q) {
+    return function(entity, action, params, message) {
+      // JSON serialization in CRM.api3 is not aware of Angular metadata like $$hash, so use angular.toJson()
+      var deferred = $q.defer();
+      var p;
+      if (_.isObject(entity)) {
+        p = CRM.api3(eval('('+angular.toJson(entity)+')'), message);
+      } else {
+        p = CRM.api3(entity, action, eval('('+angular.toJson(params)+')'), message);
+      }
+      // CRM.api3 returns a promise, but the promise doesn't really represent errors as errors, so we
+      // convert them
+      p.then(
+        function(result) {
+          if (result.is_error) {
+            deferred.reject(result);
+          } else {
+            deferred.resolve(result);
+          }
+        },
+        function(error) {
+          deferred.reject(error);
+        }
+      );
+      return deferred.promise;
+    };
+  });
+
+  angular.module('crmUtil').factory('crmLegacy', function() {
+    return CRM;
+  });
+
   // example: scope.$watch('foo', crmLog.wrap(function(newValue, oldValue){ ... }));
   angular.module('crmUtil').factory('crmLog', function(){
     var level = 0;
     return crmLog;
   });
 
+  angular.module('crmUtil').factory('crmNavigator', ['$window', function($window) {
+    return {
+      redirect: function(path) {
+        $window.location.href = path;
+      }
+    };
+  }]);
+
   angular.module('crmUtil').factory('crmNow', function($q){
     // FIXME: surely there's already some helper which can do this in one line?
     // @return string "YYYY-MM-DD hh:mm:ss"
index 95b896a512a6e94df9913f04916ecf9aa88d4b04..0726a045040cd0221783c60dc431a9fea3089462 100644 (file)
@@ -1,4 +1,7 @@
 (function(angular, CRM) {
+  // crmApp is the default application which aggregates all known modules.
+  // crmApp should not provide any significant services, and no other
+  // modules should depend on it.
   var crmApp = angular.module('crmApp', CRM.angular.modules);
   crmApp.config(['$routeProvider',
     function($routeProvider) {
@@ -7,41 +10,4 @@
       });
     }
   ]);
-  crmApp.factory('crmApi', function($q) {
-    return function(entity, action, params, message) {
-      // JSON serialization in CRM.api3 is not aware of Angular metadata like $$hash, so use angular.toJson()
-      var deferred = $q.defer();
-      var p;
-      if (_.isObject(entity)) {
-        p = CRM.api3(eval('('+angular.toJson(entity)+')'), message);
-      } else {
-        p = CRM.api3(entity, action, eval('('+angular.toJson(params)+')'), message);
-      }
-      // CRM.api3 returns a promise, but the promise doesn't really represent errors as errors, so we
-      // convert them
-      p.then(
-        function(result) {
-          if (result.is_error) {
-            deferred.reject(result);
-          } else {
-            deferred.resolve(result);
-          }
-        },
-        function(error) {
-          deferred.reject(error);
-        }
-      );
-      return deferred.promise;
-    };
-  });
-  crmApp.factory('crmLegacy', function() {
-    return CRM;
-  });
-  crmApp.factory('crmNavigator', ['$window', function($window) {
-    return {
-      redirect: function(path) {
-        $window.location.href = path;
-      }
-    };
-  }]);
 })(angular, CRM);
index 61951372725b092eb6c5c3a48ba9e1e86a82ed2c..dddd2d403615e791b0eecce569e047f53bc7ad1b 100644 (file)
@@ -4,7 +4,7 @@
     return CRM.resourceUrls['civicrm'] + '/partials/crmCaseType/' + relPath;
   };
 
-  var crmCaseType = angular.module('crmCaseType', ['ngRoute', 'ui.utils', 'crmUi', 'unsavedChanges', 'crmApp']);
+  var crmCaseType = angular.module('crmCaseType', ['ngRoute', 'ui.utils', 'crmUi', 'unsavedChanges', 'crmUtil']);
 
   // Note: This template will be passed to cloneDeep(), so don't put any funny stuff in here!
   var newCaseTypeTemplate = {
index 7595d67abd9970642fe876531f312d5c9898548c..9f5d0a72ead503bb9ae468d3d7beffdfd7c28a5d 100644 (file)
@@ -4,7 +4,7 @@
   };
 
   angular.module('crmMailing', [
-    'crmUtil', 'crmAttachment', 'ngRoute', 'ui.utils', 'crmUi', 'dialogService', 'crmApp'
+    'crmUtil', 'crmAttachment', 'ngRoute', 'ui.utils', 'crmUi', 'dialogService'
   ]); // TODO ngSanitize, unsavedChanges
 
   // Time to wait before triggering AJAX update to recipients list
index 28419384a3dc53c8640dece4185f038bb72ecb4d..142249a45a4472457766057443a0772ed415b327 100644 (file)
@@ -6,7 +6,6 @@ CRM.angular = {
     'unsavedChanges',
     'angularFileUpload',
     'dialogService',
-    'crmApp',
     'crmAttachment',
     'crmUi',
     'crmUtil',
index 67382e983c96b56cf4206b478719005276c5f749..8d35904988b0d3343a3c0f1a7d7f28630846ccb1 100644 (file)
@@ -3,7 +3,7 @@
 describe('crmMailing', function() {
 
   beforeEach(function() {
-    module('crmApp');
+    module('crmUtil');
     module('crmMailing');
   });