From a8e659741916210d929a80bd1f7f7b3b686da6e9 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sat, 3 Jan 2015 09:46:00 -0800 Subject: [PATCH] crmApp, crmUtil - Tighten scope of crmApp. Move services to crmUtil. Avoid cyclic dependencies. 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 | 40 ++++++++++++++++++++++++++++++ js/angular-crmApp.js | 40 +++--------------------------- js/angular-crmCaseType.js | 2 +- js/angular-crmMailing.js | 2 +- tests/karma/modules.js | 1 - tests/karma/unit/crmMailingSpec.js | 2 +- 6 files changed, 46 insertions(+), 41 deletions(-) diff --git a/js/angular-crm-util.js b/js/angular-crm-util.js index b67659cf46..d27875a2b8 100644 --- a/js/angular-crm-util.js +++ b/js/angular-crm-util.js @@ -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; @@ -33,6 +65,14 @@ 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" diff --git a/js/angular-crmApp.js b/js/angular-crmApp.js index 95b896a512..0726a04504 100644 --- a/js/angular-crmApp.js +++ b/js/angular-crmApp.js @@ -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); diff --git a/js/angular-crmCaseType.js b/js/angular-crmCaseType.js index 6195137272..dddd2d4036 100644 --- a/js/angular-crmCaseType.js +++ b/js/angular-crmCaseType.js @@ -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 = { diff --git a/js/angular-crmMailing.js b/js/angular-crmMailing.js index 7595d67abd..9f5d0a72ea 100644 --- a/js/angular-crmMailing.js +++ b/js/angular-crmMailing.js @@ -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 diff --git a/tests/karma/modules.js b/tests/karma/modules.js index 28419384a3..142249a45a 100644 --- a/tests/karma/modules.js +++ b/tests/karma/modules.js @@ -6,7 +6,6 @@ CRM.angular = { 'unsavedChanges', 'angularFileUpload', 'dialogService', - 'crmApp', 'crmAttachment', 'crmUi', 'crmUtil', diff --git a/tests/karma/unit/crmMailingSpec.js b/tests/karma/unit/crmMailingSpec.js index 67382e983c..8d35904988 100644 --- a/tests/karma/unit/crmMailingSpec.js +++ b/tests/karma/unit/crmMailingSpec.js @@ -3,7 +3,7 @@ describe('crmMailing', function() { beforeEach(function() { - module('crmApp'); + module('crmUtil'); module('crmMailing'); }); -- 2.25.1