简体   繁体   English

Angular-通用图像服务与特定资源(例如,用户,图像,动物)图像服务

[英]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.

相关问题 如何检查浏览器是否缓存了特定资源(例如图像)? - How to check if a particular resource (e.g. image) is cached by the browser? 修复了无法在旧版移动浏览器上渲染的图像 - Fixed image not rendering on older mobile browsers e.g. blackberry JQXChart-如何获取图表的图像二进制(例如dataURL) - JQXChart - how to get image binary (e.g. dataURL) of a chart 具有JavaScript和CSS的动画图像(例如非GIF)? - Animated image (e.g. non-GIF) with JavaScript and CSS? HTML5中的旋转轮图像(例如轮盘赌轮)? - Spin wheel image in HTML5 (e.g., roulette wheel)? Safari img元素不会使用blob URL将从服务(例如,Dropbox)检索的图像渲染为ArrayBuffer - Safari img element won't render image retrieved from service (e.g., Dropbox) as ArrayBuffer using blob URL 在不调整大小的情况下减小图像宽度(例如动态裁剪图像) - Reduce Image Width Without Resizing (e.g. crop image dynamically) 是否可以从html flash对象中读取图像数据? 例如,通过画布drawImage - Is it possible to read image data from an html flash object? E.g. via canvas drawImage jQuery / Java脚本:通过链接选项(例如,形状,颜色)自定义图像/头像 - Jquery/Java Script: Customise an Image/Avatar by chainging options (e.g. Shape, Colour) 找出加载的图像是否无效(例如404错误页面) - Find out if loaded image is not valid (e.g. is a 404 error page)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM