5

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?

1
  • any fix for your issue ? Actually this works with the hasMany relationship only for one-to-one there is a problem.
    – sudhanshu
    Feb 20, 2013 at 10:02

1 Answer 1

3

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);
      }
    });
  },
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.