I’m using Ember 1.0-pre4.
I have two models in one-to-one relationship:
App.Lesson = DS.Model.extend
timeslot: DS.belongsTo 'App.Timeslot'
App.Timeslot = DS.Model.extend
lesson: DS.belongsTo 'App.Lesson'
I have an adapter configured to embed timeslot into lesson upon saving:
App.Adapter = DS.RESTAdapter.extend()
App.Adapter.map App.Lesson,
timeslot: { embedded: 'always' }
App.Store = DS.Store.extend
revision: 11
adapter: App.Adapter.create()
Then I create a lesson and a time slot and try to save them:
lesson = App.Lesson.createRecord
group: group
lesson.set('timeslot', App.Timeslot.createRecord())
lesson.store.commit()
But upon saving nothing is embedded and I see to POST requests, one for lesson and one for timeslot.
How do I tell Ember to always embed timeslot into lesson?
I think this is a bug and you should report it. Sifting through the source code and doing some tests reveals that
createRecorddoes not take into account theembeddedconfiguration at all. This configuration is only used for the serialization and deserialization process.When you make a call to createRecord, a record is added to a bucket,
created, and oncommitember-datasimply fires an ajax post on every record in the bucket.So to get back to your code, to
ember-data, you created two records and on commits it will fire an ajax post call for theLessonobject withTimeslotembeddedin it, AND will also, in a subsequent call, fires another ajax post forTimeslot, the last remaining record in the bucket.Unless, someone with better understanding of the inners of ember-data contradicts my point, i tend to believe that, again, that this is a bug.
Here’s the last code called when you commit the transaction.