在ember-data中保留父级和嵌入式记录

时间:2013-06-27 11:38:11

标签: ember.js ember-data

我有以下型号:

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();

这是一个错误还是我做错了什么?已经花了相当长的时间......

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

this.transaction.createRecord(App.Company, {})

代码片段创建单独的公司实体。对于它有一个后期行动真的是一个惊喜吗?

答案 1 :(得分:0)

据我记得,当时使用的Ember Data的(旧)版本从未实际支持过。较新的版本无论如何处理这种情况,所以我说它已经过时并关闭它。