mehulkar
mehulkar

Reputation: 4974

Ember + Ember Data: How to rollback a record after calling deleteRecord()

I have an Em.ArrayController with a bunch of records in it. They are all controlled by an itemController.

App.ColorsController = Em.ArrayController.extend({
  itemController: 'color', 
  actions: {
    discardChanges: function() {
      this.get('content').forEach(function(color) { color.rollback(); }
      // also tried an arrayComputed property like this:
      // deletedRecords: Em.computed.filterBy('content', 'isDeleted', true);
    }
  }
});

If I call deleteRecord() on one of the models (from an action in the itemController, the model is removed from the model of the ArrayController.

App.ColorController = Em.ObjectController.extend({
  actions: {
    deleteColor: function() {
      // does not send a `DELETE` request, only
      // changes the state of the record
      this.get('content').deleteRecord();
    }
  } 
});

Remember that deleteRecord doesn't submit a network request, it merely transitions the state of the object to deleted.uncommitted.

Do I need to manually retain some sort of handle on this object after deleting it? Or is there some way for the ArrayController to access items in this state.

I've attempted to filter the content of the ArrayController by isDeleted.

Upvotes: 2

Views: 581

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

Your array controller is being backed by a filter. Filters are active, in that they automatically add/remove records as the store has active records added/removed. (fyi, find('foo') returns the all filter).

You can copy the contents to a non active collection/array which won't automagically add/remove the models (you will have to do everything manually). The easiest place would be to override the setupController and add a property onto your controller, which can be accessed in your template.

App.FooRoute = Em.Route.extend({
  model: function(){
    this.store.find('foo');
  },
  setupController: function(controller, model){
    this._super(controller, model);
    controller.set('staticFoos', model.toArray());
  }
});

Upvotes: 2

Related Questions