CRM-15578 - Add crmStatus() service as a simpler wrapper for CRM.status()
authorTim Otten <totten@civicrm.org>
Wed, 3 Dec 2014 01:17:16 +0000 (17:17 -0800)
committerTim Otten <totten@civicrm.org>
Wed, 3 Dec 2014 02:34:37 +0000 (18:34 -0800)
js/angular-crm-util.js
js/angular-crmMailing2.js

index 4e6decf8268723c93bf2d2b0289878d60b9592a9..e0b296fdd51b3228ee54badf2006f054e1273600 100644 (file)
@@ -2,6 +2,14 @@
 (function (angular, $, _) {
   angular.module('crmUtil', []);
 
+  // Adapter for CRM.status which supports Angular promises (instead of jQuery promises)
+  // example: crmStatus('Saving', crmApi(...)).then(function(result){...})
+  angular.module('crmUtil').factory('crmStatus', function($q){
+    return function(options, aPromise){
+      return CRM.toAPromise($q, CRM.status(options, CRM.toJqPromise(aPromise)));
+    };
+  });
+
   // crmWatcher allows one to setup event listeners and temporarily suspend
   // them en masse.
   //
index c8453cbd02464c44e24349a24a9c2cfbebc02975..d75bfbf9fe3f665d7074a5245d9dde65775073d3 100644 (file)
@@ -3,7 +3,7 @@
     return CRM.resourceUrls['civicrm'] + '/partials/crmMailing2/' + relPath;
   };
 
-  var crmMailing2 = angular.module('crmMailing2', ['ngRoute', 'ui.utils', 'crmUi', 'dialogService']); // TODO ngSanitize, unsavedChanges
+  var crmMailing2 = angular.module('crmMailing2', ['crmUtil', 'ngRoute', 'ui.utils', 'crmUi', 'dialogService']); // TODO ngSanitize, unsavedChanges
 
   // Time to wait before triggering AJAX update to recipients list
   var RECIPIENTS_DEBOUNCE_MS = 100;
@@ -55,7 +55,7 @@
     });
   });
 
-  crmMailing2.controller('EditMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location, crmMailingMgr, crmFromAddresses, $q) {
+  crmMailing2.controller('EditMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location, crmMailingMgr, crmFromAddresses, crmStatus) {
     $scope.mailing = selectedMail;
     $scope.crmMailingConst = CRM.crmMailing;
     $scope.crmFromAddresses = crmFromAddresses;
 
     // @return Promise
     $scope.submit = function submit() {
-       // CRM.status doesn't work with Angular promises, so do backflips
-       var p = crmMailingMgr.submit($scope.mailing);
-       var p2 = CRM.status(null, CRM.toJqPromise(p));
-       return CRM.toAPromise($q, p2);
+      return crmStatus({start: ts('Submitting...'), success: ts('Submitted')},
+        crmMailingMgr.submit($scope.mailing)
+      );
     };
     // @return Promise
     $scope.save = function save() {
-      // CRM.status doesn't work with Angular promises, so do backflips
-      var p = crmMailingMgr.save($scope.mailing);
-      var p2 = CRM.status(null, CRM.toJqPromise(p));
-      return CRM.toAPromise($q, p2);
+      return crmStatus(null,
+        crmMailingMgr.save($scope.mailing)
+      );
     };
     // @return Promise
     $scope.delete = function cancel() {
-      // CRM.status doesn't work with Angular promises, so do backflips
-      var p = crmMailingMgr.delete($scope.mailing);
-      var p2 = CRM.status({start: ts('Deleting...'), success: ts('Deleted')}, CRM.toJqPromise(p));
-      return CRM.toAPromise($q, p2);
+      return crmStatus({start: ts('Deleting...'), success: ts('Deleted')},
+        crmMailingMgr.delete($scope.mailing)
+      );
     };
     $scope.leave = function leave() {
       window.location = CRM.url('civicrm/mailing/browse/unscheduled', {