Add belongsTo relationship (embedded) and perform transaction.commit when model instances to add are loaded


#1

My current problem is related to adding belongsTo relationships on a new created App.Task model instance. If the belongsTo relationships assignedUser and reporter are not added

/*
  this.set('assignedUser',App.User.find().objectAt(1));
  this.set('reporter',App.User.find().objectAt(2));
  newTask.set('assignedUser',this.get('assignedUser'));
  newTask.set('reporter',this.get('reporter'));
*/ (commenting out this lines of code)

the call of create Task is working and no errors occurs. If createTask is called trying to set the belongsTo relationship the code works as expected with the exception of this errors:

Uncaught Error: Attempted to handle event loadedData on while in state rootState.loaded.updated.uncommitted. Called with undefined ember-data.js:3520

Uncaught Error: Attempted to handle event loadedData on while in state rootState.loaded.updated.uncommitted. Called with undefined ember-data.js:3520

Uncaught Error: Attempted to handle event loadedData on while in state rootState.loaded.updated.uncommitted. Called with undefined ember-data.js:3520

This log messages shows that the model instances are not loaded completely or are not in the right state. I assume that I have to ensure that the User model instances are both loaded before the commit should take place.

I know that the loading of the model instances are performed in a asynchrony way, but I don’t know best best emberdata - idiomatic way to call a transaction.commit after the completion of multiple loading activities. Is there a way to define the loading of the User model instances as a part of the task - creation transaction ???

Some hints to this common use case would be greatly appreciated !

TaskController::processTask Task controllers.js:212

User Model didLoad on: Hurtig models.js:17

User Model didLoad on: Baumert

App.User = DS.Model.extend({
  lastName: attr('string'),
  firstName: attr('string'),
  email: attr('string'),
  phone: attr('string'),
  name: function(){
      return (this.get('firstName') +' '+this.get('lastName'));
  }.property('firstName','lastName'),
 didLoad: function(){
     console.log('User Model didLoad on:',this.get('lastName'));
 }
});

   App.Task = DS.Model.extend({
      taskType: attr('string'),

      // TODO Anpassung der Services
      //taskState: DS.belongsTo(App.TaskState),
      taskState: attr('string'),

      priority: attr('string'),
      created: attr('number'),
      reporter: DS.belongsTo(App.User),
      assignedUser: DS.belongsTo(App.User),
      subject: attr('string'),
      description: attr('string'),
      taskComments: DS.hasMany(App.TaskComment),
      incidentRef: DS.belongsTo(App.Incident),

    });


  var Adapter = DS.RESTAdapter.extend();

  Adapter.map(App.Task, {

   reporter: { embedded: 'always' },
   assignedUser: { embedded: 'always' },
   taskComments: { embedded: 'always' }

 });


var  adapter = Adapter.create({namespace: 'restws'});

App.Store = DS.Store.extend({
revision: 12,
    adapter: adapter
});

createTask is a method in the TaskController. This method is called inside a TaskController action.

createTask: function(){

    // TODO hinzufuegen des Manager und Festlegung des
    // assigned User
    // Mit richtigem Incident verbinden usw. !!!
    var transaction = this.get('store').transaction();
    if (transaction) {
        this.set('transaction', transaction);
        var newTask = transaction.createRecord(App.Task,

            { "taskType" : "INCIDENT_PROCESSING",
                "taskState" : "NEW",
                "priority" : "HIGH",
                "created" : 0,
                "reporter" : null,
                "assignedUser" : null,
                "subject" : "Test programmatische Erzeugung !!!",
                "taskComments" : [],
                "incidentRef" : "51695452ef8669f87104670e"
            }
        );


        this.set('assignedUser',App.User.find().objectAt(1));
        this.set('reporter',App.User.find().objectAt(2));


        newTask.set('assignedUser',this.get('assignedUser'));
        newTask.set('reporter',this.get('reporter'));
    } else { console.log('store.transaction() returned null');
    }

    this.get('transaction').commit();
    console.log('TaskController::processTask Task');
}