Backbone.js的&安培; REST API资源关系和放大器; interraction [英] Backbone.js & REST API resources relationship & interraction

查看:267
本文介绍了Backbone.js的&安培; REST API资源关系和放大器; interraction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在被搭载Backbone.js的单个页面的Web应用程序所消耗的小REST API

I have a small REST API that is being consumed by a single page web application powered by Backbone.js

有API提供,因此,骨干应用程序使用两种资源类型。这些文章和评论。这两种资源具有不同的终端和存在来自每个物品到的该项目的所有注释的位置的链接。

There are two resource types that the API provides, and therefore, the Backbone app uses. These are articles and comments. These two resources have different endpoints and there is a link from each of the articles to the location of all the comments for that item.

这是我现在面临的问题是,在我的web应用程序的文章列表中,我想能够显示每个文章的评论的数量。鉴于这只会是可能的,如果我也得到了评论列表,在当前的设置,需要我做一个API请求,以获得初始的文章列表,另一个用于每个物品要能算数注释。如果,例如,有100条,因此101的HTTP请求有必要填充单一角度的问题。

The problem that I'm facing is that, on the article list in my web app I would like to be able to display the number of comments for each article. Given that that would only be possible if I also get the comments list, on the current setup, would require me to make one API request to get the the initial article list and another one for each of the articles to be able to count the number of comments. That becomes a problem if, for instance, there are 100 articles, and therefore 101 HTTP requests would be necessary to populate one single view.

我能想到的,现在的解决方案是:

The solutions I can think of right now are:

1。包括评论中的初始数据的文章像这样的请求。

{
  {
    "id": 1,
    "name": "Article 1",
    ...
    "comments": {
      {
        "id": 1,
        "text": "some comment"
      },
      {
        "id": 2,
        "text": "some comment"
      },
      ...
    }
  },
}

在这种情况下,现在的问题是:怎么可能来解析意见作为一个独立的意见收集,它不包括成文章模型

The question in this case is: How is it possible to parse the "comments" as a separate comments collection and not include it into the article model?

2。包括一些元数据里面的文章像这样响应:

2. to include some metadata inside the articles response like so:

{
  {
    "id": 1,
    "name": "Article 1",
    ...
    "comments": 13
  },
}

选项是提出了一个问题:我应该如何处理模型的解析,这样,一方面元信息是可用的,并且在另一方面,意见属性是不是一骨干会尝试执行更新呢?

Option that raises the question: how should I handle the parse of the model so that, on one hand the meta information is available, and on the other hand, the "comments" attribute is not one Backbone would try to perform updates on?

我觉得有可能是另一种解决方案,符合REST的理念,因为这是我的思念,所以如果你有任何其他建议,请让我知道。

I feel there might be another solution, compliant with the REST philosophy, for this that I'm missing, so if you have any other suggestion please let me know.

推荐答案

我会建议使用替代2和让服务器返回
一个的子集的被认为有用的文章属性
与文章收集资源打交道时,应用程序
(比如在可达/条)。

I would suggest using alternative 2 and have the server return a subset of the article attributes that are deemed useful for applications when dealing with the article collection resource (perhaps reachable at /articles).

及其所有评论的全文成员资源(无论是
它们被存储在单独的表中的后端)将是
可在 /用品/:ID

The full article member resource with all its comments (whether they are stored in separate tables in the backend) would be available at /articles/:id).

从一个Backbone.js的角度,你可能想要把
在收集的资源,比方说, ArticleCollection 这将
转换各构件(当前与属性的子集)
文章模式。

From a Backbone.js point of view you probably want to put the collection resource in a, say, ArticleCollection which will convert each member (currently with a subset of the attributes) to Article models.

当用户选择全额查看文章,你把它
出从 ArticleCollection 并调用填充
它完全。

When the user selects to view an article in full you pull it out from the ArticleCollection and invoke fetch to populate it in full.

关于如何处理所包含额外/虚拟属性做
在收集资源( /条)之类的评论数和
其他可能的usefult聚集,我看到几个选择:

Regarding what to do with extra/virtual attributes that are included in the collection resource (/articles) like the comment count and possibly other usefult aggregations, I see a few alternatives:


  1. 文章#初始化您可以从拉出来的那些属性
    并且将它们存储为在制品上的元数据。这种方式内置的
    Backbone.Model#的toJSON 将看不到它们。

  2. 让他们在每一个模型,覆盖的属性部分
    Backbone.Model#的toJSON 来exlcude他们当序列化的文章

  1. In Article#initialize you can pull those out from the attributes and store them as meta-data on the article. This way the built-in Backbone.Model#toJSON will not see them.
  2. Keep them in the attributes section of each model and override Backbone.Model#toJSON to exlcude them when "serializing" an Article.

在atlernative 1,文章#commentCount()助手可以返回
this._commentCount || this.get(意见)。长度,使其工作
两个部分和满载的文章。

In atlernative 1, an Article#commentCount() helper could return this._commentCount || this.get('comments').length to make it work on both partially and fully loaded articles.

有关满载文章你可能要转换的
嵌套的注释数组转换成一个完全成熟的 CommentCollection 反正
并存储在 this._comments ,所以我不认为这是不寻常
有你的模型存储的其他东西,直接在模型实例,
外面的属性的哈希值。

For a fully loaded Article you would probably want to convert the nested comments array into a full-blown CommentCollection anyway and store that in this._comments so I don't think it is that unusual to have your models store additional stuff directly on the model instance, outside of its attributes hash.

这篇关于Backbone.js的&安培; REST API资源关系和放大器; interraction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆