将JSON序列化为Ember-Data(有效载荷中的两种不同型号) [英] Serialize JSON into Ember-Data (two different models in payload)
问题描述
我的JSON来自服务器,如下所示:
data:{
user:{
地址:{
id:id,
city:city,
street:street,
.......
}
名称:name,
......
}
身份验证:{
令牌:令牌,
id:id
}
}
在Ember中我有一个模型 auth ,它应该代表从服务器获取的认证模型。由于我在服务器和Ember存储区中使用不同的名称,所以我用一个方法typeForRoot(root)编写了一个序列化程序,其中我将服务器认证映射到Ember auth 。
问题是这个typeForRoot实际上从来没有被调用过,我没有一点点理由为什么。
这里是Ember认证模型:
从ember-data导入DS;
导出默认DS.Model.extend({
token:DS.attr('string')
});
这里是我的序列化程序:
从ember-data导入DS;
从ember导入Ember;
导出默认DS.RESTSerializer.extend({
typeForRoot:function(root){
//'authentication'应该成为'auth'
var subRoot = root.substring(0,root.length - 10);
// _super将'认证'归一化为'auth'
返回this._super(subRoot);
}
});
我的用户模型被正确保存在商店中(我是使用一个单独的,基于模型的,串行器的用户),但我收到消息警告:有效载荷中遇到验证,但没有找到型号名称验证(解析的型号名称使用shop- app @ serializer:user:.typeForRoot(authentication))
当认证模型需要存储在商店中的 auth 模型中时。 / p>
有谁知道如何克服这个问题。
谢谢,
米兰
我假设 typeForRoot
你已经实现了在auth序列号?如果您注意到错误,它会指出:
shop-app @ serializer:user:.typeForRoot(authentication)
这特别指向用户序列号。
我假设你正在请求用户模型返回数据中的用户模型和Auth模型 - 由于您使用用户适配器和序列化程序来执行此操作,您需要在用户端上实现 typeForRoot
逻辑序列号。
I have JSON coming from the server which looks like:
data: {
user: {
address: {
id: "id",
city: "city",
street: "street",
.......
}
name: "name",
......
}
authentication: {
token: "token",
id: "id"
}
}
In Ember I have a model auth which should represent the authentication model that I get from the server. Since I have different names for same model on the server and in the Ember store, I wrote a serializer with one method typeForRoot(root) where I just map server authentication to Ember auth. Problem is that this typeForRoot is actually never invoked, and I do not have a slightly idea why.
Here is Ember auth model:
import DS from 'ember-data';
export default DS.Model.extend({
token: DS.attr('string')
});
And here is my serializer:
import DS from 'ember-data';
import Ember from 'ember';
export default DS.RESTSerializer.extend({
typeForRoot: function(root) {
// 'authentication' should become 'auth'
var subRoot = root.substring(0, root.length - 10);
// _super normalizes 'authentication' to 'auth'
return this._super(subRoot);
}
});
My user model is being properly saved in the store (I'm using a separate, model based, serializer for user), but I get the message WARNING: Encountered "authentication" in payload, but no model was found for model name "authentication" (resolved model name using shop-app@serializer:user:.typeForRoot("authentication"))
when authentication model needs to be saved in auth model in the store.
Does anyone know how can I overcome this problem. Thanks, Milan
I assume the typeForRoot
you've implemented is on the auth serialiser? If you notice the error, it states:
shop-app@serializer:user:.typeForRoot("authentication")
This particularly points to the User serialiser.
I assume you're doing a request for the User model which returns the User model and Auth model in the data - since your using the User adapter and Serialiser to do this, you would need to implement the typeForRoot
logic on the User serialiser.
这篇关于将JSON序列化为Ember-Data(有效载荷中的两种不同型号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!