坚持以烬数据父母和嵌入式记录
问题描述:
我有以下型号:坚持以烬数据父母和嵌入式记录
App.Company = DS.Model.extend({
name: DS.attr('string'),
accounts: DS.hasMany('App.Account', {
inverse: 'company'
})
});
App.Account = DS.Model.extend({
login: DS.attr('string'),
first_name: DS.attr('string'),
last_name: DS.attr('string'),
email: DS.attr('string'),
password: DS.attr('string'),
password_confirmation: DS.attr('string'),
company: DS.belongsTo('App.Company')
});
公司被定义为嵌入的帐户:
DS.RESTAdapter.map('App.Account', {
company: { embedded: 'always' }
});
当我创建一个新帐户,公司数据被正确地嵌入到帐户数据中,并且我看到了我期望在服务器端的POST请求:
Started POST "/accounts" for 127.0.0.1 at 2013-06-27 13:30:53 +0200
Processing by AdminUsersController#create as JSON
Parameters: {"account"=>{"login"=>"fsdfdf", "first_name"=>"fgdfgh", "last_name"=>"[email protected]", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "company"=>{"name"=>"gfdhgtrhzrh"}}}
但是,我也看到了公司本身的附加POST请求:
Started POST "/companies" for 127.0.0.1 at 2013-06-27 13:30:53 +0200
Processing by CompaniesController#create as JSON
Parameters: {"company"=>{"name"=>"gfdhgtrhzrh"}}
我设立的型号如下:
this.transaction = this.get('store').transaction();
var account = this.transaction.createRecord(App.Account, {});
account.set('company', this.transaction.createRecord(App.Company, {}));
当用户点击保存,我只是犯交易:
this.transaction.commit();
这是一个错误还是我做错了什么?花了相当一段时间,已经...
感谢您的帮助!
答
据我记得,在我用过的Ember Data的(旧)版本中从来没有真正支持过。新版本处理这种情况不同,所以我会说这是过时的,并关闭它。
答
this.transaction.createRecord(App.Company, {})
的代码片段创建单独的公司实体。是否真的有这样的惊喜有一个后续行动呢?
我通过将嵌入式配置更改为DS.RESTAdapter.map('App.Account',{company:{embedded:'load'}});不知道为什么这实际上...... – marcoow
我认为这是一个与belongsTo hasMany行为如预期。有很多(甚至没有孩子)你实际上使用关系来创建一个子记录,但与belongsTo的关系为null,所以你不能用this.get('company')创建它。createRecord –
我创建了一个拉请求我认为修复它 - 我不明白为什么它不应该工作fir属于就像它为hasMany所做的那样:https://github.com/emberjs/data/pull/1067 – marcoow