JSON API响应和ember模型名称 [英] JSON API response and ember model names

查看:113
本文介绍了JSON API响应和ember模型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于JSON API响应键类型与Ember模型名称匹配的一个快速问题。

A quick question about the JSON API response key "type" matching up with an Ember model name.

如果我有一个模型,说models / photo。 js,我有一个像/ photos的路由,我的JSON API响应看起来像这样

If I have a model, say "models/photo.js" and I have a route like "/photos", my JSON API response looks like this

{
  data: [{
    id: "298486374",
    type: "photos",
    attributes: {
      name: "photo_name_1.png",
      description: "A photo!"
    }
  },{
    id: "298434523",
    type: "photos",
    attributes: {
      name: "photo_name_2.png",
      description: "Another photo!"
    }
  }]
}

我在假设我的模型名称应该是单数,但这个错误弹出

I'm under the assumption that my model name should be singular but this error pops up

Assertion Failed: You tried to push data with a type 'photos' but no model could be found with that name

这当然是因为我的模型被命名photo

This is, of course, because my model is named "photo"

现在在JSON API规范中有一个注释,内容是这个规范对于转折规则是不可知的,所以类型的值可以是复数或单数但是,在整个实施过程中应该一贯使用相同的值。

Now in the JSON API spec there is a note that reads "This spec is agnostic about inflection rules, so the value of type can be either plural or singular. However, the same value should be used consistently throughout an implementation."

所以,

tl; dr做事情的Ember方式是否具有模型名称和JSON API响应键类型都是单数的?或者只要匹配,就不重要?

tl;dr Is the "Ember way" of doing things to have both the model names and the JSON API response key "type" both be singular? or does it not matter as long as they match?

推荐答案

JSON API串行化器期望复数类型。 来自指南的有效载荷示例。

JSON API serializer expects plural type. Payload example from guides.

由于 modelNameFromPayloadKey 功能单个键,它适用于单数类型:

Since modelNameFromPayloadKey function singularizes key, it works with singular type:

// as is
modelNameFromPayloadKey: function(key) {
  return singularize(normalizeModelName(key));
}

但反向操作 payloadKeyFromModelName 多元化模型名称,应该更改,如果您在后端使用单一类型:

but inverse operation payloadKeyFromModelName pluralizes model name and should be changed, if you use singular type in your backend:

// as is
payloadKeyFromModelName: function(modelName) {
  return pluralize(modelName);
}

重要的是内部的Ember Data JSON API格式与一个由JSONAPISerializer使用。 Store.push 期望单一类型,JSON API序列化程序期望复数

It is important that the internal Ember Data JSON API format differs a bit from the one used by JSONAPISerializer. Store.push expects singular type, JSON API serializer expects plural.

讨论

... ED内部使用camelCased属性和单数类型,无论使用哪种适配器/序列化程序。

"...ED uses camelCased attributes and singular types internally, regardless of what adapter/serializer you're using.

当您使用JSON API适配器/序列化程序,我们希望用户能够使用jsonapi.org上可用的示例,并且可以正常工作,大多数用户从来不必关心内部格式,因为序列化器处理它们的工作。

When you're using the JSON API adapter/serializer we want users to be able to use the examples available on jsonapi.org and have it just work. Most users never have to care about the internal format since the serializer handles the work for them.

这是在指南中记录的, http://guides.emberjs.com/v2.0.0/models/pushing-records-into-the-store/
...

This is documented in the guides, http://guides.emberjs.com/v2.0.0/models/pushing-records-into-the-store/ ..."

这篇关于JSON API响应和ember模型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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