1

I am using AngularJS $resource to list, create, and update resources and it works fine. For example:

getFoo = (fooId) => {
  const Foo = $resource("api/foos/:fooId);
  return Foo.get({fooId});
};

This returns a Foo instance that has a Foo.$promise I can use to see when the request finishes.

But what if I want to delete a Foo?

deleteFoo = (fooId) => {
  const Foo = $resource("api/foos/:fooId);
  return Foo.delete({fooId});
};

All the examples I can find show Foo.delete({fooId}), but never tell what it returns. How can I get a promise to know when deletion is successfull?

Does Foo.delete({fooId}) return a Foo $resource with a Foo.$promise, just like get()? But that would be odd to have a resource of a thing I've just deleted. Does it return the $promise itself? Does it return the underlying $http object?

In short, how can I get a promise from Foo.delete({fooId}) to know when deletion has been successful?

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • 1
    A downvote without a comment is basically useless if you are trying to help guide someone to frame a proper question. – James Drinkard Apr 19 '18 at 20:32
  • I'm trying to do the very same thing. I want to call a resource method before the user leave the page, this needs to a be synchronous call and block until the response is returned. From the docs the method returns an empty instance (whatever that means) that has $promise as a parameter. I've tried calling then(onsuccess()) on this but the success callback never seems to be executed – JamesP May 03 '18 at 14:41

1 Answers1

0

From the documentation:

The action methods on the class object or instance object can be invoked with the following parameters:

"class" actions without a body: Resource.action([parameters], [success], [error])
"class" actions with a body: Resource.action([parameters], postData, [success], [error])
instance actions: instance.$action([parameters], [success], [error])

A delete would be an action without a body, so if you are wanting to verify your delete was succesfull, then you need to send success and error with the action.

Here is an example using an entity id:

 var entityResource = $resource('/api/:entityType/:entityId', 
                                   {type:'@entityType', id: '@entityId'});
    
    entityResource.delete({type: $scope.entityType, id: entityId}, success, failed);

Your success or failed would look something like this:

var success = function (result) {
    $scope.remove($scope.table.data, idColumn, entityId);
};

var failed = function (result) {
    alert("You action has failed with: " + result);
Community
  • 1
  • 1
James Drinkard
  • 15,342
  • 16
  • 114
  • 137
  • 2
    This question is about using `$resource`, not `$http`. This question is not about a failure response. Specifically the question was what `$resource.delete()` returns on success. If you provided me _extra_ information, that's great, but I'm having trouble finding the answer to my specific question in your response. – Garret Wilson Apr 20 '18 at 19:31