我有一個兩個模型,用戶和主題,這兩個模型都依賴api數據的應用程序應用程序。該JSON返回不在該燼期望的格式,所以我對每一個模型 -Ember.js加載路線時出錯
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));
}
});
當我嘗試訪問我的用戶在瀏覽器中,我得到這個錯誤了類似的串行:
Assertion failed: Error while loading route: TypeError: Cannot read property 'array' of undefined
誰能幫助?
編輯:在extractArray功能「型」指的是JSON的root屬性,例如,如果我的JSON是
{
"user": {
name: "xyz",
email: "[email protected]"
}
}
的「類型」是「用戶」。
並在extractArray功能是什麼 '類型' 時,代碼正在執行?,放置一個調試器或一個日誌,看看它有什麼價值。 – fanta