Updated crmApi to be latest from Angular.tpl
authorPeter Haight <peterh@giantrabbit.com>
Tue, 30 Dec 2014 22:56:59 +0000 (14:56 -0800)
committerPeter Haight <peterh@giantrabbit.com>
Tue, 30 Dec 2014 22:56:59 +0000 (14:56 -0800)
When I did the last bit of merging, I missed the changes to this
function in Angular.tpl which I've moved here.

js/angular-crmApp.js

index ed7d1c29221e4c1538968c55cf3fef638502836a..95b896a512a6e94df9913f04916ecf9aa88d4b04 100644 (file)
@@ -7,14 +7,31 @@
       });
     }
   ]);
-  crmApp.factory('crmApi', function() {
+  crmApp.factory('crmApi', function($q) {
     return function(entity, action, params, message) {
-      // JSON serialization in CRM.api3 is not aware of Angular metadata like $$hash
-      if (CRM._.isObject(entity)) {
-        return CRM.api3(eval('('+angular.toJson(entity)+')'), 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 {
-        return CRM.api3(entity, action, eval('('+angular.toJson(params)+')'), message);
+        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() {