resource.$delete call

2,213 views
Skip to first unread message

Carmen Popoviciu

unread,
Mar 15, 2013, 11:50:57 AM3/15/13
to ang...@googlegroups.com
Hi,

According to the AngularJS doc "The actions saveremove and delete are available on an instance of the resource class as methods with the $ prefix." (http://docs.angularjs.org/api/ngResource.$resource). I had no problems implementing this for save actions, but for remove or delete I just can't get it to work. I have looked over the code in angular-resource.js and noticed that when I try to make a resource.$delete call the data passed to the call is undefined, even though my resource object exists. Looking a bit further, I saw that data is set to undefined because hasBody is false, where:
var data = hasBody ? this : undefined;
var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH';

So I'm a bit confused. Am I overlooking something or is the documentation wrong and calling resource.$delete or resource.$remove is just not possible?

I haven't included a fiddle, but for the sake of the example, my resource looks something like this:

factory('Report', function($resource) {
        return $resource('/user/:personId/report/:reportId', {reportId: '@id'}, {
            update: {method: 'PUT'}
        })
    })
 
and the delete call in my controller:

$scope.deleteReport = function() {
 report.$delete({personId: person_id}, function(response){ 
// do smth 
}, function(response){ 
 // do smth else
});

where:
var report = Report.get({personId: person_id, reportId: report_id});

The AngularJS version I am using is v1.0.2 
 

eddelplus

unread,
Mar 16, 2013, 7:19:41 AM3/16/13
to ang...@googlegroups.com
I've been analysing the angular-resource.js code a bit more closely recently. There is a distinction between methods that transfer data and those that do not. Interestingly DELETE does not transfer data (i.e. the JSON encoded object). Thus calling $delete or $remove as instance methods does not make too much sense. I would recommend calling remove() as a (static) method on the Class (Report), supplying both personId and reportId explicitly. That has worked in my latest experiments with $resource.

Pawel Kozlowski

unread,
Mar 16, 2013, 7:23:12 AM3/16/13
to ang...@googlegroups.com
Hi!

On Sat, Mar 16, 2013 at 12:19 PM, eddelplus <joc...@eddelbuettel.net> wrote:
> There is a distinction between methods that transfer data and those that do
> not. Interestingly DELETE does not transfer data (i.e. the JSON encoded
> object).

Yes, this is correct. The reason is that behavior of HTTP DELETE
method with body is not defined very precisely and different
server-side endpoints treat the sent body differently (some of the
simply ignoring it).

> Thus calling $delete or $remove as instance methods does not make
> too much sense. I would recommend calling remove() as a (static) method on
> the Class (Report),

I don't think I can agree with this statement. Calling $remove (or
$delete) on instance method makes sense and is simply saying "remove
me".
The trick here is that data in the instance can influence URL sent to
the server (I'm taking about @ params here).

Cheers,
Pawel


--
Looking for bootstrap-based widget library for AngularJS?
http://angular-ui.github.com/bootstrap/

Carmen Popoviciu

unread,
Mar 18, 2013, 5:37:48 AM3/18/13
to ang...@googlegroups.com
OK. Thank you both for the reply :)
Reply all
Reply to author
Forward
0 new messages