yes, but "restful" api is a little off.  Meaning your save, update, and 
delete methods should not have to specific the action in the url since the 
HTTP method defines that for you.  Also, the path should be consistent, not 
"persons"  plural and then "person" singular:

GET /persons---> show all person
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:

$resource('http://localhost:9000/persons/:id', 
  { 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){
       var Persons =  $resource('http://localhost:9000/persons', {},{query: 
{method:'GET'}, isArray:true};
       var Person = 
$resource('http://localhost:9000/person<http://localhost:9000/persons>
/:id/:methodName', 
                                   { 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.id = '1234';
   $scope.currentPerson.$get();
}


This is untested code, but should get you going in the right direction.


On Sunday, March 2, 2014 8:52:39 AM UTC-7, Adil Ramdan wrote:
>
> i have a model call it "Person" and i have some url Rest for manage the 
> "Person"
> GET /persons---> show all person
> GET /person/:id--->show person by id
> PUT /person/save --> save person
> PUT /person/update --> update person
> DELETE /person/delete -> delete person
>
> i know one factory handle multiple request method but with same url.. 
> is my case can be handle with one factory?
>
> appServices.factory('Person', ['$resource',
>   function($resource){
>     return $resource('http://localhost:9000/persons', {},{query: 
> {method:'GET'}, isArray:true}
>     });
>   }]);
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to