Javascript Ember.JSON JSON API混乱
我目前正在尝试使用Ember版本2.2.0和Ember数据2.2.1创建一个简单的Ember应用程序。我创建restapi是为了遵循jsonapi规范v1.0,这是一个使用GET/articles检索资源数组的示例Javascript Ember.JSON JSON API混乱,javascript,json,rest,ember.js,Javascript,Json,Rest,Ember.js,我目前正在尝试使用Ember版本2.2.0和Ember数据2.2.1创建一个简单的Ember应用程序。我创建restapi是为了遵循jsonapi规范v1.0,这是一个使用GET/articles检索资源数组的示例 { "links" : { "self" : "http://localhost:8080/test-app/rest/articles" }, "data" : [ { "id" : "5666157634499515eb7e13f0",
{
"links" : {
"self" : "http://localhost:8080/test-app/rest/articles"
},
"data" : [
{
"id" : "5666157634499515eb7e13f0",
"type" : "articles",
"attributes" : {
"title" : "test"
}
},
{
"id" : "5666157634499515eb7e13f1",
"type" : "articles",
"attributes" : {
"title" : "test2"
}
},
...
]
}
我的文章路线如下所示:
// routes/articles.js
...
export default Ember.Route.extend({
model() {
return this.store.findAll('article');
}
});
模型:
// models/article.js
...
export default DS.Model.extend({
title: DS.attr(),
text: DS.attr(),
url: DS.attr(),
date: DS.attr()
});
我完全按照教程()进行操作。然而,当我打开视图时,我得到一个警告:
WARNING: Encountered "data" in payload, but no model was found for model name "datum" (resolved model name using test-app@serializer:-rest:.modelNameFromPayloadKey("data"))
并且不呈现任何数据。我可以正确地看到请求和返回的数据,但不知何故,它的解释是错误的
我的问题是:怎么了?我试图坚持默认API,不编写任何适配器或序列化程序,但它不起作用
--
更新:我将“链接”部分添加到响应对象,现在我收到两条警告:
WARNING: Encountered "links" in payload, but no model was found for model name "link" (resolved model name using test-app@serializer:-rest:.modelNameFromPayloadKey("links"))
WARNING: Encountered "data" in payload, but no model was found for model name "datum" (resolved model name using test-app@serializer:-rest:.modelNameFromPayloadKey("data"))
--
更新2:所以我通过重新构造API响应使其看起来像:
{
"articles" : [ {
"id" : "5666157634499515eb7e13f0",
"attributes" : {
"title" : "test"
}
}, {
"id" : "5666141034499511aea5f43c",
"attributes" : {
"title" : "test"
}
},
...
}
- 将“数据”重命名为“文章”,它特定于请求的资源和请求类型(获取一个,获取多个)
- 从资源对象中删除“属性”,现在直接包括所有属性
- 删除了根对象中“articles”旁边的所有键
更新3:修复了这个问题。看看我的答案(我还不能接受)。基本上,我使用了错误的REST适配器。ember cli使用RestaAdapter生成默认适配器,必须将其更改为JSONAPIAdapter。问题在于ember在默认情况下使用RestaAdapter。要将此响应格式与JSON API一起使用,必须使用JSONAPIAdapter。因此,在app/adapters/application.js中:
export default DS.JSONAPIAdapter.extend({
...
});
并将每个API响应的内容类型更改为规范中描述的application/vpn.API+json