[英]Angular - Generic image service vs resource-specific (e.g., user, image, animal) image services
I'm having a little trouble wrapping my mind around a problem and could use a little help. 我在解决问题时遇到了一些麻烦,可能需要一些帮助。
I'm developing an app in Rails using Angular.js for my client-side MVC framework and have created a service by which Angular can get images. 我正在为我的客户端MVC框架使用Angular.js在Rails中开发一个应用程序,并创建了一个服务,Angular可以通过该服务获取图像。 It's pretty straightforward:
这很简单:
var ImageServices = angular.module("ImageServices", ["ngResource"]);
ImageServices.factory("Image", ["$resource",
function ($resource) {
return $resource("/images/:id.json", {}, {
"query" : { method: "GET", params: {}, isArray: true },
"update" : { method: "PUT", params: { id: "@id" } },
"remove" : { method: "DELETE" }
});
}
]);
The above service is probably a bit too generic for my purposes. 就我的目的而言,以上服务可能有点过于通用。 What I'd like for the service to provide me is all images associated with a given user .
我想要该服务提供给我的是与给定用户关联的所有图像。 I suppose I could accomplish this by nesting the desired image data within the user's data, like this:
我想可以通过将所需的图像数据嵌套在用户数据中来完成此操作,如下所示:
user = {
// user attributes
blah: "blah",
//image objects
images: [
{}
]
}
Or maybe I could somehow write code in Rails that will return all images associated with the user, thus: 或者,也许我可以以某种方式在Rails中编写代码,该代码将返回与用户关联的所有图像,因此:
http://localhost:3000/users/1/images.json
Or maybe I could just make a service for each type of data that I'd like returned to Angular. 或者也许我可以为要返回Angular的每种数据类型提供服务。
Architecturally, I'm not sure what makes sense. 在架构上,我不确定什么有意义。 Could someone provide some insight into what the best possible path might be?
有人可以提供一些最佳路径可能的见解吗? If there are no hard and fast answers, I'm not opposed to reading a book on the topic if you could recommend one.
如果没有一成不变的答案,如果可以推荐的话,我不反对读一本有关该主题的书。 Thanks!
谢谢!
The $resource service is kind of like a wrapper for the $http service. $ resource服务有点像$ http服务的包装器。 It has methods built into it.
它内置了一些方法。
services.factory('Images', ['$resource',
function($resource) {
return $resource('/images/:id', {id: '@id'});
}]);
The @id tells it to pick the id field from its object and use that as the id parameter. @id告诉它从其对象中选择id字段,并将其用作id参数。 So in your case, you could do {id: '@user'}.
因此,根据您的情况,您可以执行{id:'@user'}。
The above resource give you access to: 以上资源使您可以访问:
Images.get() //GET Single JSON
Images.save() //POST Single JSON
Images.query() //GET JSON Array
Images.remove() //DELETE Single JSON
Images.delete() //DELETE Single JSON
Images.get({id: 15}) //will make return all images for a user through a call to /images/15.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.