JSON API 响应和 ember 模型名称
JSON API response and ember model names
关于 JSON API 响应键 "type" 与 Ember 模型名称匹配的快速问题。
如果我有一个模型,比如 "models/photo.js" 并且我有一个像“/photos”这样的路线,我的 JSON API 响应看起来像这样
{
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!"
}
}]
}
我假设我的型号名称应该是单数,但弹出此错误
Assertion Failed: You tried to push data with a type 'photos' but no model could be found with that name
这当然是因为我的模型被命名为"photo"
现在在 JSON API 规范中有一条注释显示 "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 way"处事的模型名称和JSONAPI响应键"type"都是单数吗?还是只要匹配就无所谓?
JSON API 序列化程序需要复数类型。 Payload example from guides.
由于modelNameFromPayloadKey
函数将键单数化,因此它适用于单数类型:
// as is
modelNameFromPayloadKey: function(key) {
return singularize(normalizeModelName(key));
}
但逆运算 payloadKeyFromModelName
使模型名称复数化,如果您在后端使用单数类型,则应更改:
// as is
payloadKeyFromModelName: function(modelName) {
return pluralize(modelName);
}
重要的是内部 Ember 数据 JSON API 格式与 JSONAPISerializer 使用的格式略有不同。 Store.push
期望 singular 类型,JSON API 序列化器期望 plural.
来自discussion:
"...ED 在内部使用驼峰式属性和单数类型,无论您使用什么 adapter/serializer。
当您使用 JSON API adapter/serializer 时,我们希望用户能够使用 jsonapi.org 上提供的示例并让它正常工作。大多数用户永远不必关心内部格式,因为序列化程序会为他们处理工作。
这在指南中有记载,http://guides.emberjs.com/v2.0.0/models/pushing-records-into-the-store/
..."
根据您的用例,您可以尝试 pushPayload
instead of push
. As the documentation 建议,它会进行一些规范化;就我而言,它涵盖了 "plural vs. singular" 问题。
关于 JSON API 响应键 "type" 与 Ember 模型名称匹配的快速问题。
如果我有一个模型,比如 "models/photo.js" 并且我有一个像“/photos”这样的路线,我的 JSON API 响应看起来像这样
{
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!"
}
}]
}
我假设我的型号名称应该是单数,但弹出此错误
Assertion Failed: You tried to push data with a type 'photos' but no model could be found with that name
这当然是因为我的模型被命名为"photo"
现在在 JSON API 规范中有一条注释显示 "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 way"处事的模型名称和JSONAPI响应键"type"都是单数吗?还是只要匹配就无所谓?
JSON API 序列化程序需要复数类型。 Payload example from guides.
由于modelNameFromPayloadKey
函数将键单数化,因此它适用于单数类型:
// as is
modelNameFromPayloadKey: function(key) {
return singularize(normalizeModelName(key));
}
但逆运算 payloadKeyFromModelName
使模型名称复数化,如果您在后端使用单数类型,则应更改:
// as is
payloadKeyFromModelName: function(modelName) {
return pluralize(modelName);
}
重要的是内部 Ember 数据 JSON API 格式与 JSONAPISerializer 使用的格式略有不同。 Store.push
期望 singular 类型,JSON API 序列化器期望 plural.
来自discussion:
"...ED 在内部使用驼峰式属性和单数类型,无论您使用什么 adapter/serializer。
当您使用 JSON API adapter/serializer 时,我们希望用户能够使用 jsonapi.org 上提供的示例并让它正常工作。大多数用户永远不必关心内部格式,因为序列化程序会为他们处理工作。
这在指南中有记载,http://guides.emberjs.com/v2.0.0/models/pushing-records-into-the-store/ ..."
根据您的用例,您可以尝试 pushPayload
instead of push
. As the documentation 建议,它会进行一些规范化;就我而言,它涵盖了 "plural vs. singular" 问题。