Ember-Data可以从一个JSON有效载荷处理两个模型吗? [英] Can Ember-Data handle two models from one JSON payload?
问题描述
我的JSON来自服务器,如下所示:
I have JSON coming from the server which looks like:
data: {
user: {
address: {
id: "id",
city: "city",
street: "street",
.......
}
name: "name",
......
}
authentication-token: {
token: "token",
id: "id"
}
}
将这两个模型(用户,身份验证令牌)存储在同名的ember store中。当我从服务器获取上述响应时,模型用户被成功保存,但是模型认证令牌根本不会保存到存储。当我将数据(在适配器中)记录在数据传递给序列化程序之前,我看到JSON具有Ember-Data期望的结构。我不知道问题是Ember-Data不能一次处理两个模型成功,然后将其保存到相应的模型,或者其他的。想法?
The idea is to store this two models (user, authentication-token) in ember store under the same names. When I gat the above mentioned response from a server, model user is saved successfully, but model authentication-token does not get saved to the store at all. When I log the data (in the adapter) before the data is passed to serializer I see that JSON has a structure which Ember-Data expects. I don't know whether the problem is that Ember-Data cannot handle two models in success at one time, and then save it to the corresponding models, or something else. Ideas?
推荐答案
现在 it 都对我有意义。当然,这是您的最后一个问题。无论如何,ember数据的RESTAdapter无法处理。如果您要求一个单一的资源用户
,它最多希望这个用户是一个单一的答案。任何其他资源可能都是side-loaded必须是一个数组。请求的单数用户
可以是用户
键下的记录或数组中的第一个条目取消用户
键。其他(元数据除外)必须是复数。
Now it all makes sense to me. Of course, this was the problem in your last question. Anyway, ember-data's RESTAdapter can't handle this. If you're requesting a singular resource user
it expects at most this user as a singular answer. Any other resource that may be "side-loaded" has to be an array. The requested singular user
can either be a record under the user
key or the first entry in an array unter the users
key. Everything else (except meta data) has to be plural.
如果您要求复数资源用户
主要响应必须在用户
之下,任何不属于 _
_users
键。
In case you're requesting a plural resource users
, the main response must be under the users
key, any sideloaded users that aren't part of the response prfixed with _
under the _users
key.
对于你的例子,这意味着你的json必须格式如下: / p>
For your example all this means that your json must be formatted like this:
data: {
user: {
address: {
id: "id",
city: "city",
street: "street",
.......
}
name: "name",
......
}
authentication-tokens: [{
token: "token",
id: "id"
}]
}
如果您无法更改服务器,确定RESTAdapter通过规范化来规范化JSON数据串行器。
If you can't change your server, you'd have to configure the RESTAdapter to normalize the JSON data through normalize of the Serializer.
这篇关于Ember-Data可以从一个JSON有效载荷处理两个模型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!