2013-06-27 24 views
17

我有以下型號:堅持以燼數據父母和嵌入式記錄

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

這是一個錯誤還是我做錯了什麼?花了相當一段時間,已經...

感謝您的幫助!

+0

我通過將嵌入式配置更改爲DS.RESTAdapter.map('App.Account',{company:{embedded:'load'}});不知道爲什麼這實際上...... – marcoow

+0

我認爲這是一個與belongsTo hasMany行爲如預期。有很多(甚至沒有孩子)你實際上使用關係來創建一個子記錄,但與belongsTo的關係爲null,所以你不能用this.get('company')創建它。createRecord –

+0

我創建了一個拉請求我認爲修復它 - 我不明白爲什麼它不應該工作fir屬於就像它爲hasMany所做的那樣:https://github.com/emberjs/data/pull/1067 – marcoow

回答

0

據我記得,在我用過的Ember Data的(舊)版本中從來沒有真正支持過。新版本處理這種情況不同,所以我會說這是過時的,並關閉它。

0

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

的代碼片段創建單獨的公司實體。是否真的有這樣的驚喜有一個後續行動呢?