Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I code a "finally" with AngularJS resource version 1.2.3?

Tags:

angularjs

I have the following code using AngularJS resource:

var entityResource = $resource('/api/:et/', {
                            et: $scope.entityType
                     });
entityResource.save(data,
   function (result) {
      // code
      $scope.modal.submitDisabled = false;
   }, function (result) {
      // code
      $scope.modal.submitDisabled = false;
   });

Is there something like a finally that I can use so I can put the code to disable (and my other code) outside of the success and error? Also can I now use .success and .error or do I still have to code as two functions inside () ?

I did notice changes in 1.2.3 but I am not sure if I understand how these apply.

like image 891
Samantha J T Star Avatar asked Dec 06 '13 16:12

Samantha J T Star


1 Answers

There Should be a $promise property on your entityResource. You should be able to set finally there.

entityResource.$promise['finally'](function(){
  // finally do something
});

see docs

update: You could do something like this:

//Using the promise on your resource.
function success(){/**success*/};
function error(){/**failure*/};
function last(){/**finally*/};
entityResource.save(data,success,error).$promise.finally(last);

By using angular's $q and .then You will invoke a $digest cycle. Which will update your view, which is usually fantastic.

like image 195
calebboyd Avatar answered Nov 15 '22 08:11

calebboyd