我有一个“居住”对象,住所有很多细节。从服务器返回数据时,如何使用序列化程序“extractSingle”映射其关系?我有以下内容,我收到错误“无法获取未定义或空引用的属性'toString'”。有人能告诉我这里我做错了什么吗?
App.Residence = DS.Model.extend({
residence_ID: attr('number'),
house_Number: attr('string'),
street: attr('string'),
city_ID: attr('number'),
state_ID: attr('number'),
zip: attr('string'),
apt: attr('string'),
client_ID: attr('number'),
client: null,
lead: null,
projectDetails: DS.hasMany('projectDetail')
});
App.ResidenceSerializer = DS.WebAPISerializer.extend({
primaryKey: 'residence_ID',
extractSingle: function (store, primaryType, payload) {
var primaryTypeName = primaryType.typeKey;
var typeName = primaryTypeName,
type = store.modelFor(typeName);
var data = {};
data[typeName] = payload;
data.projectDetails = [];
var normalizedArray = payload.projectDetails.map(function (nav) {
data.projectDetails.push(nav);
}, this);
payload.projectDetails = payload.projectDetails.mapProperty('projectDetailID');
payload = data;
return this._super.apply(this, arguments);
},
normalizeHash: {
projectDetails: function (hash) {
hash.residence_ID = hash.id;
hash.id = hash.projectDetailID;
return hash;
},
residence: function (hash) {
hash.residence_ID = hash.id;
return hash;
}
}
});
这是从服务器返回的JSON
{
"residence_ID":3532,
"house_Number":"243",
"street":"Main St.",
"city_ID":2,
"state_ID":33,
"zip":"11111",
"apt":"35",
"client_ID":3598,
"projectDetails":[{"projectDetailID":4947,"residence_ID":3532"}, {"projectDetailID":4947,"residence_ID":3532}]
}
答案 0 :(得分:1)
return this._super.apply(this, arguments);
你最后扔掉所有的工作并返回超级实现。你可以return data;
。
另请注意,arguments
payload = data;
中的引用不会更新