我可以使用灰烬数据正常(默认情况下轨)JSON响应?
我工作的一个项目使用 Ember公司
/ Ember公司-数据
并有相关的/已经存在服务,这是提供API和JSON响应。
I'm working on a project using Ember
/Ember-Data
and there is a related/already existing service which is provide API with JSON response.
我的项目必须与该服务进行交互,而是来自API的响应是成才象下面这样:
My project must interact with that service, but the response from that API is someting like below:
{ "id": 39402, "name": "My Name" }
或
[ {"id": 38492, "name": "Other Name" } ]
没有人:
或人数:
由要求Ember公司 - 数据
compatable响应。
there is no person:
or persons:
that is required by Ember-Data
compatable response.
如何使用上 Ember公司-数据
这种反应不改变对服务或没有建立API网关?
How can I using this response on Ember-Data
without change on the service or without build API gateway?
Ember公司-数据
使用 DS.RestAdapter
,这反过来又利用从 DS.JSONSerializer
延伸序列化,提取和按摩自带数据 DS.RESTSerializer
从服务器
Ember-Data
uses DS.RestAdapter
, which in turn uses DS.RESTSerializer
which extends from DS.JSONSerializer
for serializing, extracting and massaging data that comes in from the server.
由于你的情况,你已经在你的有效载荷中的数据,所有你需要的阅读的数据覆盖提取物
的方法来做在 JSONSerializer
这其实是很简单的。
Since in your case you already have the data in your payload, all you need to do for reading the data is override extract
method in the JSONSerializer
which is actually quite simple.
如果您使用的是烬-CLI
(你应该:)),你的 person.js
文件位置里面的应用程序/串行
目录将如下所示。
If you are using ember-cli
(which you should :)), your person.js
file located inside your app/serializers
directory would look as follows.
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
extract: function(store, primaryType, payload) {
return payload;
}
});
如果您不使用烬-CLI
,你可以做到以下几点:
If you are not using ember-cli
, you can do the following:
App.PersonSerializer = DS.JSONSerializer.extend({
extract: function(store, primaryType, payload) {
return payload;
}
});