[英]Models are not serialized with Ember.js and WebApiAdapter
我正在嘗試將Ember.js MVC4 Spa模板與我自己的模型一起使用,但無法正常工作。
目前,服務器端代碼正在運行。 瀏覽器結果正確。 但是Ember-Data或自定義的WebApi-序列化器無法准備數據。
我有兩種模型:患者:
App.Patient = DS.Model.extend();
App.Patient.reopen({
patientId: DS.attr('number'),
firstName: DS.attr('string'),
lastName: DS.attr('string'),
aufenthalte: DS.hasMany('aufenthalt'), //, { async: true }
fullName: function () {
return this.get('firstName') + ' ' + this.get('lastName');
}.property('firstName', 'lastName'),
});
App.PatientSerializer = DS.WebAPISerializer.extend({
primaryKey: 'patientId',
// ember-data-1.0.0-beta2 does not handle embedded data like they once did in 0.13, so we've to update individually if present
// once embedded is implemented in future release, we'll move this back to WebAPISerializer.
// see https://github.com/emberjs/data/blob/master/TRANSITION.md for details
extractArray: function (store, primaryType, payload) {
var primaryTypeName = primaryType.typeKey;
var typeName = primaryTypeName,
type = store.modelFor(typeName);
var data = {};
data[typeName] = payload;
data.aufenthalte = [];
var normalizedArray = payload.map(function (hash) {
hash.aufenthalte.map(function (aufenthalt) {
data.aufenthalte.push(aufenthalt);
});
hash.aufenthalte = hash.aufenthalte.mapProperty('aufenthaltId');
return hash;
}, this);
payload = data;
return this._super.apply(this, arguments);
},
normalizeHash: {
patient: function (hash) {
hash.patientId = hash.id;
return hash;
}
}
});
Aufenthalt:
App.Aufenthalt = DS.Model.extend({
aufenthaltId: DS.attr('number'),
name: DS.attr('string'),
patientId: DS.attr('number'),
patient: DS.belongsTo('patient'),
});
App.AufenthaltSerializer = DS.WebAPISerializer.extend({
primaryKey: 'aufenthaltId',
normalizeHash: {
aufenthalte: function (hash) {
hash.aufenthaltId = hash.id;
return hash;
},
}
});
當我從控制器中獲取“患者”列表時,數據模型已正確填充(我可以在Chrome Ember插件中檢查它。)當我用“患者ID”點擊“操作”時,出現錯誤:“加載路線時出錯:TypeError:無法設置未定義的屬性'store'
謝謝!
您是否在app / routes文件夾中添加了正確的路由器,在app / controllers文件夾中添加了控制器,以及相應的視圖和模板? 隨意psot到您的示例解決方案的鏈接,以便我下載並查看。
===更新2/22/2014 ===我修復了代碼。 您應該能夠從https://www.dropbox.com/s/4j3vbczqr4nx68m/EmberVSTemplateModified.zip下載修改后的解決方案。 您應該在兩個目錄上進行預備操作以查看更改。 我需要更改一些位置以使其適合您的方案,包括:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.