以下一些例子,似乎我们可以注入一个工厂,它将包含一个这样的休息服务的端点
services.factory('Recipe', ['$resource', function($resource) { return $resource('/recipes/:id', {id: '@id'}); }]);这看起来很棒,但是想像我有其他端点,即/ users /:id和/ groups /:id,你可以想像不同端点的数量会增加。
所以对于每个端点都有一个不同的工厂是好的做法,所以有..
services.factory('Recipe', ['$resource',............ services.factory('Users', ['$resource',............. services.factory('Groups', ['$resource',...............还是有另一个推荐的方法?
我真的没有看到它的问题,但它会迫使我创建很多工厂,只是为了处理不同的端点。
任何帮助或指导真的很受赞赏
谢谢
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
最满意答案
这是一个偏好的问题。
但是没有什么可以阻止您将所有资源整合到一个工厂内,如:
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(); ... }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(); ... }
更多推荐
发布评论