GET /persons/:id--->show person by id
POST /persons/:id--> save person
PUT /persons/:id--> update person
DELETE /persons/:id -> delete person
If your api was as such, then the $resource object is defined like:
{ id: '@id'}, //assuming the object returned has property "id", else '@person_id' or whatever it is called
{
query: { method:'GET', isArray:true },
save: { method: 'POST' },
update: { method: 'PUT' },
delete: { method: 'DELETE' }
}
If you cannot change the restful API, you will have to create two $resource objects in your factory and return the proper one based on the method:
appServices.factory('PersonService', ['$resource',
function($resource){
{ id: '@id'}, //assuming the object returned has property "id", else '@person_id' or whatever it is called
//these will create the url correctly by defining the variable methodName in the parameters - just make sure id is defined when calling
{
get: { method:'GET', isArray: false },
save: { method: 'PUT', { params: {methodName: 'save'} } },
update: { method: 'PUT', { params: {methodName: 'update'} } },
delete: { method: 'DELETE', { params: {methodName: 'delete'} } },
}
//Now you return both and your controller calls the one it needs
return {
all: Persons,
one: return new Person()
}
});
}]);
function MyCtrl($scope, PersonService){
$scope.personList = PersonService.all;
$scope.personList.get();
$scope.currentPerson = PersonService.one;
$scope.currentPerson.$get();
}
This is untested code, but should get you going in the right direction.