Rollback of parent record does not delete child records

Rollback of the parent record does not seem to delete any new child records that could be added to it. Not sure if this is a bug or feature.

App.Company = DS.Model.extend({
  name: DS.attr('string'),
  employees: DS.hasMany('App.Employee')
});

App.Employee = DS.Model.extend({
  name: DS.attr('string'),
  company: DS.belongsTo('App.Company')
})

var company = App.Company.find(1);
//Initially: company.get('employees.length') => 0

var employee = company.get('employees').createRecord();
//Now: company.get('employees.length') => 1

employee.get('transaction').rollback();
//Still: company.get('employees.length') =>1
//Also: employee.get('isDeleted') => true

company.get('transaction').rollback();
//Even now: company.get('employees.length') =>1

//How do I rollback such that company.get('employees.length') == 0
+5
source share
1 answer

I have a rollbackAll () method in my application that does a depth traversal using get and promises and then collapses them all back before rolling back the parent. It looks like this:

rollbackAll: function() {
  // recursively rollback on all children and self
  this.get('children').then(function (children) {
    children.forEach(function(child) {
      child.rollbackAll();
    });
  });
  this.rollback();
}

I should mention this because there is a recursive relationship in my application (i.e. a tree of objects, each of which may have its own parent).

, "" rollbackAll, , , rollbackAll() . ( , ).

0

All Articles