I\'m writing an AngularJS client application that would interact with a REST server.
To manage the client / server interaction I\'m using the $resource abstraction. Actu
I prefer the second approach:
var resources = angular.module("myapp.resources", ['ngResource']);
resources.factory('Constants', [
function() {
return {
RESOURCE_URL: "http://www.example.com/rest"
}
}
]);
resources.factory('Rest', ['Constants', '$resource', function(C, $resource) {
return {
Users: $resource(C.RESOURCE_URL + '/users/:id', {
id: '@id',
}, {})
, Posts: $resource(C.RESOURCE_URL + '/posts/:user', {
user: '@'
}, {})
}
}]);
When you have several resources, become very annoying to manage all the dependencies in your controller. That way, all you have to do is inject a single one. It is also, in my opinion, easier to understand when reading the controller:
$scope.user = Rest.Users.get({id: 1});
is more understandable that
$scope.user = Users.get({id: 1});