[英]How should I lay my model in ember in case of this hasMany relations?
我從服務器發送的JSON看起來像這樣,
{"selectFill":{"user":[{"userId":"Gru","department":"Villain"},{"userId":"Sulley","department":"Scare"},{"userId":"Popeye","department":"Sailer"}]}}
我的模型看起來像這樣
App.SelectFill = DS.Model.extend({
users: DS.hasMany('user',{
embedded: true
})
});
App.User = DS.Model.extend({
userId: DS.attr('string'),
department: DS.attr('department')
});
App.SelectFillSerializer = DS.WebAPISerializer.extend({
extractArray: function (store, type, payload, id, requestType) {
var primaryTypeName = type.typeKey;
var typeName = primaryTypeName,
type = store.modelFor(typeName);
var selectFill = [];
selectFill.push(payload.selectFill);
var newi = {};
newi[typeName] = selectFill;
return this._super(store, type, newi, id, requestType);
}
});
我在Chrome調試器中用ember檢查器檢查了代碼,我可以在SelectFill中看到1條記錄,但是它是空的。 用戶記錄也為空。 您能告訴我,我在這里做錯了嗎?我是Ember的新手,我試圖進入Ember源代碼和頭腦,然后我的頭開始旋轉。 每個人都還通過深入研究Ember源代碼來調試其代碼嗎?
您需要將數據轉換為這種格式,並且所有記錄都需要有ID(如果您希望userId為用戶ID,請從模型定義中刪除它並將其添加到適配器中)
{
"selectFill":
{
id:'something',
"users":['Gru', 'Sulley','Popeye']
},
"users":[
{"userId":"Gru","department":"Villain"},
{"userId":"Sulley","department":"Scare"},
{"userId":"Popeye","department":"Sailer"}]
}
App.UserAdapter = DS....extend({
primaryKey: 'userId'
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.