crmUtil - Alphabetize services
[civicrm-core.git] / js / angular-crmApp.js
CommitLineData
416abe87
PH
1(function(angular, CRM) {
2 var crmApp = angular.module('crmApp', CRM.angular.modules);
3 crmApp.config(['$routeProvider',
4 function($routeProvider) {
5 $routeProvider.otherwise({
6 template: ts('Unknown path')
7 });
8 }
9 ]);
3c8dd2bf 10 crmApp.factory('crmApi', function($q) {
416abe87 11 return function(entity, action, params, message) {
3c8dd2bf
PH
12 // JSON serialization in CRM.api3 is not aware of Angular metadata like $$hash, so use angular.toJson()
13 var deferred = $q.defer();
14 var p;
15 if (_.isObject(entity)) {
16 p = CRM.api3(eval('('+angular.toJson(entity)+')'), message);
416abe87 17 } else {
3c8dd2bf 18 p = CRM.api3(entity, action, eval('('+angular.toJson(params)+')'), message);
416abe87 19 }
3c8dd2bf
PH
20 // CRM.api3 returns a promise, but the promise doesn't really represent errors as errors, so we
21 // convert them
22 p.then(
23 function(result) {
24 if (result.is_error) {
25 deferred.reject(result);
26 } else {
27 deferred.resolve(result);
28 }
29 },
30 function(error) {
31 deferred.reject(error);
32 }
33 );
34 return deferred.promise;
416abe87
PH
35 };
36 });
37 crmApp.factory('crmLegacy', function() {
38 return CRM;
39 });
40 crmApp.factory('crmNavigator', ['$window', function($window) {
41 return {
42 redirect: function(path) {
43 $window.location.href = path;
44 }
45 };
46 }]);
47})(angular, CRM);