我从服务器发送的 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'
});
关于javascript - 如果存在这种 hasMany 关系,我应该如何将我的模型放入 ember 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20602346/