Ember.js data associations not saved

Let's say I have 2 models:

App.Address = DS.Model.extend({ street: DS.attr('string'), person: DS.belongsTo('App.Person') }) App.Person = DS.Model.extend({ name: DS.attr('string'), addresses: DS.hasMany('App.Address') }) 

Now i create man

 App.person = App.Person.createRecord({name: 'Bill'}); App.store.commit(); 

If I try to add an address to a person like this

 address = App.Address.createRecord({street: '123 Fake Street'}); App.person.get('addresses').pushObject(address); 

and make a transaction

 App.store.commit(); 

The new address will be saved, however the person object will not be recognized as changed; even though the list of identifiers went from

 { ... "addresses": [] } 

to

 { ... "addresses": [3] } 

Is there any way to let ember-data know that my face object has been changed and needs to be saved?

Edit: Here is a jsfiddle illustrating the problem.

+7
source share
3 answers

Currently (version 4 for ember-data) there is no way to do this. An object is saved only on the side belonging to the side, since it has a foreign key, therefore, if you set the value on the side belonging to the side of the relation, it should be saved. The next version of ember data should solve this problem.

+1
source

The work I found for this is to simply add id manually:

 App.Address = DS.Model.extend({ street: DS.attr('string'), personID: DS.attr('string'), person: DS.belongsTo('App.Person') }) 

and then when you submit your data, set the personId field and it will simulate what the user field looks like it should send.

+1
source

How about writing your own serializer for this, see http://ember-website.thomasboyt.com/api/data/modules/data-serializer.html

+1
source

All Articles