我有一个带有两个模型的用户和主题的ember应用程序,这两个模型都依赖于api来获取数据。返回的JSON不是ember期望的格式,因此我为每个模型都有一个自定义序列化程序 -
var foo = {
'App.Subject': {'single':'subject','array':'subjects'}
};
App.SubjectSerializer = DS.RESTSerializer.extend({
extractSingle: function(store, type, payload, id, requestType) {
var p = {};
p[foo[type]['single']] = payload;
return this._super(store, type, p, id, requestType);
},
extractArray: function(store, type, payload, id, requestType) {
var p = {};
p[foo[type]['array']] = payload;
return this._super(store, type, p, id, requestType);
},
serializeIntoHash: function(hash, type, record, options) {
Ember.merge(hash, this.serialize(record, options));
}
});
这完美无缺。但是,我的用户模型有一个类似的序列化器 -
var foo = {
'App.User': {'single':'user','array':'users'}
};
App.UserSerializer = DS.RESTSerializer.extend({
extractSingle: function(store, type, payload, id, requestType) {
var p = {};
p[foo[type]['single']] = payload;
return this._super(store, type, p, id, requestType);
},
extractArray: function(store, type, payload, id, requestType) {
var p = {};
p[foo[type]['array']] = payload;
return this._super(store, type, p, id, requestType);
},
serializeIntoHash: function(hash, type, record, options) {
Ember.merge(hash, this.serialize(record, options));
}
});
当我尝试在浏览器中访问我的用户时,出现此错误:
断言失败:加载路径时出错:TypeError:无法读取 property' array'未定义的
有人可以帮忙吗?
编辑:'键入'在extractArray函数中引用了JSON的root属性,例如,如果我的JSON是{
"user": {
name: "xyz",
email: "xyz@wxy.com"
}
}
'类型'将是'用户'。
答案 0 :(得分:1)
mappings[type]
未定义。什么是映射?
<强>单强>
p[type.typeKey] = payload;
<强>阵列强>
p[Ember.String.pluralize(type.typeKey)] = payload;