我有以下型号:
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"=>"fsfdsfdsfsd@fgfdgdfgf.de", "email"=>"dsfdsgds@frgdfgfgdfg.de", "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();
这是一个错误还是我做错了什么?已经花了不少时间了...
感谢帮助!