目前正在使用Ember-cli开发一个应用程序,并且对具有2个单词和关系的模型存在一些困难。
模型居民档案
//models/residents-profile.js
DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
picture: DS.attr('string'),
phone: DS.attr('string'),
gender: DS.attr('string'),
residentsAccount: DS.belongsTo('residentsAccount')
}
**模范居民账户**
//models/residents-account.js
DS.Model.extend({
email: DS.attr('string'),
password: DS.attr('string'),
profileId: DS.attr('number'),
residentsProfile: DS.belongsTo('residentsProfile', this.profileId),
});
居民路线上的模型挂钩:
//routes/residents.js
model: function() {
return Ember.RSVP.hash({
residentsProfile: this.store.find('residentsProfile'),
residentsAccount: this.store.find('residentsAccount')
})
}
当我尝试只获取居民配置文件时,我收到一个错误"residents.index无法读取属性'typeKey'"
但是,如果我从居民配置文件中删除关系密钥,并且只调用居民配置文件,则数据将被正确提取。
我正在使用RESTAdapter和Restful API,
模型被单独返回,服务器的响应如下:
获取/居民配置文件{"居民配置文件":[{"id":20,"picture":null,"phone":null,"firstName":"Rocky","lastName":"Balboa","blockId":null,"unitId":null,"createdAt":"2014-09-17 19:54:28","updatedAt":"2014-09-17 19:54:28","居民帐户":[5]}]}
获取/居民帐户{"居民帐户":[{"id":5,"电子邮件":"rocky@balboainc.me","admin":false,"居民":真的,"profileId":20,"createdAt":"2014-09-17 19:54:29","updatedAt":"2014-09-17 19:54:29","居民配置文件":[20]}]}
编辑
除了@Kingpin2k提出的更改外,我还以以下方式使用了setupcontroller:
setupController: function(controller,
controller.set('residentsAccount', models.residentsAccount);
controller.set('residentsProfile', models.residentsProfile);
}
现在一切正常。
三个问题,两个关系都应该是异步的(因为它们不会在同一响应中返回)
residentsAccount: DS.belongsTo('residentsAccount', {async: true})
residentsProfile: DS.belongsTo('residentsProfile', {async:true})
来自它们的两个json响应都应该是单个id,而不是数组
{
"residentsProfiles":[
{
"id":20,
"picture":null,
"phone":null,
"firstName":"Rocky",
"lastName":"Balboa",
"blockId":null,
"unitId":null,
"createdAt":"2014-09-17 19:54:28",
"updatedAt":"2014-09-17 19:54:28",
"residentsAccount": 5
}
]
}
最后,我不确定你想用this.profileId
实现什么,但它可能没有达到你想象的效果。该范围内的this
可能是窗口,这意味着你很可能会传入未定义的内容。