单个记录一对多的关系,我有两个型号的组织和站点如下:烬数据一个与EmbeddedRecordsMixin不节能上创建
网站:
export default DS.Model.extend({
...,
organization: DS.belongsTo('organization',{async:true}),
administration: DS.attr('boolean',{defaultValue: false}),
users: DS.hasMany('user',{async:true}),
totalUsers: function() {
return this.get('users').get('length');
}.property('[email protected]')
});
组织:
export default DS.Model.extend({
...,
sites: DS.hasMany('site',{async:true}),
totalSites: function() {
return this.get('sites').get('length');
}.property('[email protected]'),
type: DS.attr('string')
});
每个组织都有很多网站,但这些网站只有一个组织。我以前使用的应用程序序列化所指出here,但它停止与Ember数据1.0.0测试版10.所以我搬到了embeddedrecords混入和独立串行像下面
组织工作串行:
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
sites: {serialize: 'ids', deserialize: 'ids'}
}
});
然后在控制器上,我可以创建新的站点记录,并将新的站点记录推送到组织中,但是我的问题是组织未被保存到原始站点记录中。我的新站点控制器操作如下:
actions: {
addSite: function() {
var self = this;
var newSite = this.store.createRecord('site', {
name: this.get('name'),
abbreviation: this.get('abbreviation'),
address: this.get('address'),
city: this.get('city'),
state: this.get('state'),
postalcode: this.get('postalcode'),
country: this.get('country'),
phone: this.get('phone'),
mingrade: this.get('mingrade'),
maxgrade: this.get('maxgrade'),
organization: self.get('selectedOrg'),
administration: this.get('administration')
});
newSite.save().then(function(ns) {
self.get('selectedOrg.sites').then(function() {
self.get('selectedOrg.sites').addObject(ns).then(function() {
self.get('selectedOrg').save().then(function() {
self.transitionToRoute('site', ns);
});
});
});
});
}
}
什么可能导致组织不能保存,我知道数据在那里?如果我将belongsTo组织从async更改为not,它会保存,但会导致应用程序中其他位置的查找出现问题。我能做些什么来保存,同时也是异步的。我是否需要向网站序列化程序添加其他内容?我已经尝试在组织中添加attr,并且没有任何改变。
任何帮助将不胜感激。
这实际上是现在正在工作的部分。我换了你的代码,它有两种方式。我的问题就在此之上。在新的站点中,它不是在组织中保存到站点记录中。我无法弄清楚为什么这不起作用。 – Kris 2014-10-07 14:12:11
哦,对不起,我误解了你的问题。 selectedOrg已经有一个ID,对吗?如果没有,你可能会遇到这个问题:https://github.com/emberjs/data/issues/1542#issuecomment-56237770。但是,由于您在创建时设置它,它应该与金色数据的金丝雀版本一起工作。如果selectedOrg没有ID,则需要先将其保存。 – Kingpin2k 2014-10-07 14:49:18
selectedOrg确实有一个ID。你是对的,这是你连接的问题。运行1.0.0-beta.11 + canary.4e51ded433确实解决了这个问题,再次感谢您的帮助。 – Kris 2014-10-07 15:06:59