基于此处的文档:https://guides.emberjs.com/v2.5.0/models/customizing-serializers/#toc_jsonserializer JSONSerializer是我用于API的正确序列化程序。
尽管我收到以下错误:
ember.debug.js:28535处理路由时出错:core.admin.users 无法读取属性'type'的未定义TypeError:无法读取 属性'类型'未定义
API响应如下所示:
{
"errors": [],
"data": [
{
"id": 1,
"first_name": "Foo",
"last_name": "Bar"
},
{
"id": 2,
"first_name": "Moo",
"last_name": "Boo"
}
],
"version": 1
}
我的模型看起来像这样:
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default Model.extend({
first_name: attr('string'),
last_name: attr('string')
});
我的应用程序适配器:
import RESTAdapter from 'ember-data/adapters/rest';
export default RESTAdapter.extend({
headers: {
'Accepts': 'application/json',
'Accept': 'application/json'
}
});
我的用户适配器:
import ApplicationAdapter from './application';
export default ApplicationAdapter.extend({
buildURL: function(){
return 'core/users/foobar';
}
});
最后我的应用程序Serializer:
import JSONSerializer from 'ember-data/serializers/json';
export default JSONSerializer.extend({
});
答案 0 :(得分:0)
这不起作用,因为JSONSerializer
不期望响应中有根元素。
你必须自己扩展它。也许是这样的:
normalizeResponse (store, primaryModelClass, payload, id, requestType) {
this._super(store, primaryModelClass, payload.data, id, requestType)
}