How to insert a one-to-one relationship in Ember data?

I am using Ember 1.0-pre4.

I have two one-to-one models:

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 include a time interval in the lessons when 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 interval and try to save them:

lesson = App.Lesson.createRecord
  group: group
lesson.set('timeslot', App.Timeslot.createRecord())

lesson.store.commit()

But when saving, nothing is implemented, and I see POST requests, one for the lesson and one for the time interval.

How can I say that Ember always embeds a time interval in a lesson?

+5
source share
1 answer

, , . , createRecord embedded. .

createRecord, , created commit ember-data ajax .

, , ember-data, ajax-- Lesson Timeslot embedded , , ajax Timeslot, .

lesson = QrTimetable.Lesson.createRecord
  group: group

lesson.set('timeslot', QrTimetable.Timeslot.createRecord())
lesson.store.commit()

-, ember-, , , .

, .

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

All Articles