45

following some examples, it appears that we can inject a factory which would contain an endpoint for a rest service like so

services.factory('Recipe', ['$resource',
     function($resource) {
        return $resource('/recipes/:id', {id: '@id'});
}]);

This looks great, but imagine I have other endpoints i.e. /users/:id, and /groups/:id, as you can imagine the number of different endpoints are going to increase.

So it is good practice to have a different factory for each endpoint so having ..

services.factory('Recipe', ['$resource',............

services.factory('Users', ['$resource',.............

services.factory('Groups', ['$resource',...............

Or is there another recommended way ?

I really don't see an issue with it but its going to force me to create a lot of factories just for dealing with the different endpoints.

Any help or guidance really apprecaited

Thanks

Martin
  • 23,844
  • 55
  • 201
  • 327
  • possible duplicate of [Angularjs: a Service that serves multiple $resource urls / data sources?](http://stackoverflow.com/questions/17160771/angularjs-a-service-that-serves-multiple-resource-urls-data-sources) – mpromonet Aug 31 '14 at 08:39

1 Answers1

99

It's a matter of preference.

But nothing prevents you from consolidating all your resources inside one factory as in:

services.factory('Api', ['$resource',
 function($resource) {
  return {
    Recipe: $resource('/recipes/:id', {id: '@id'}),
    Users:  $resource('/users/:id', {id: '@id'}),
    Group:  $resource('/groups/:id', {id: '@id'})
  };
}]);

function myCtrl($scope, Api){
  $scope.recipe = Api.Recipe.get({id: 1});
  $scope.users = Api.Users.query();
  ...
}
Stewie
  • 60,366
  • 20
  • 146
  • 113
  • 2
    Ah! that makes sense, would this method be preferred over the other one? I wouldn't want to be breaking the one responsibility principle, but I think these are the same responsibility ? – Martin Jun 21 '13 at 11:15
  • Yes, I'd prefer this method, because yes, all $resources carry the same responsibility (minus the actual endpoint they are pointing to). If you were using $http to create a [more customised models](http://stackoverflow.com/a/11850027/1095616), than separate factories would be a better option. – Stewie Jun 21 '13 at 11:21
  • This way you only get API instance as well since factories are singletons. I dig it right now, but my API isn't huge yet. – Eric Novins Jan 14 '14 at 22:04
  • 1
    @Stewie Cool. But what if you want to have several entries for the Users ? Say, you have some custom method names in the resource definitions. Like, Users: $resource(... confirm: ...) and Users: $resource(... unconfirm: ...). Cheers. – Stephane Aug 11 '14 at 14:53
  • @Stewie Maybe something like this ? nodeModule.factory('NodeFirebase', ['$resource', function($resource) { return { Node: $resource( FIREBASE_URL + 'nodes/:nodeId', { nodeId: '@nodeId' }, { add: { method: 'POST', params: {}, isArray: false }, update: { method: 'PUT', params: { nodeId: '@nodeId' }, isArray: false } }), Stuff: $resource( FIREBASE_URL + 'stuff/:stuffId') } } ]); – Stephane Aug 11 '14 at 15:07
  • Yes @StephaneEybert , that would be the way to go. – Stewie Aug 11 '14 at 15:07