Multiple $resourse URL for Factory

71 views
Skip to first unread message

Adil Ramdan

unread,
Mar 2, 2014, 10:52:39 AM3/2/14
to ang...@googlegroups.com
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}
    });
  }]);

Luke Kende

unread,
Mar 2, 2014, 3:19:38 PM3/2/14
to ang...@googlegroups.com
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:

  { 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/: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.

Adil Ramdan

unread,
Mar 2, 2014, 10:38:53 PM3/2/14
to ang...@googlegroups.com
thanks very much.. you safe my day :D
god bless you :D


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/kACUWKeu0ak/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.



--
github : adilkurniaramdan
blog    : autowired.org

Reply all
Reply to author
Forward
0 new messages