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 createRecord
does not take into account the embedded
configuration 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 on commit
ember-data
simply 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 the Lesson
object with Timeslot
embedded
in it, AND will also, in a subsequent call, fires another ajax post for Timeslot
, the last remaining record in the bucket.
lesson = QrTimetable.Lesson.createRecord
group: group
lesson.set('timeslot', QrTimetable.Timeslot.createRecord())
lesson.store.commit()
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.
createRecord: function(store, type, record) {
var root = this.rootForType(type);
var data = {};
data[root] = this.serialize(record, { includeId: true });
this.ajax(this.buildURL(root), "POST", {
data: data,
context: this,
success: function(json) {
Ember.run(this, function(){
this.didCreateRecord(store, type, record, json);
});
},
error: function(xhr) {
this.didError(store, type, record, xhr);
}
});
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With