joshfarrant
joshfarrant

Reputation: 1099

Rolling back a model after failed delete doesn’t update view

I'm trying to catch and handle a failed delete request by rolling back the model and having it remain in the view, but I'm having problems. I've got the following in my controller's delete action:

model.delete();

model.save().then(function() {

  alert('Deleted!');

}, function(err) {

  alert('Delete failed...');
  model.rollback();

});

This seems to work fine, I can see that the model quickly disappears from the store and then is instantly added back in, however the problem is that it just doesn't reappear in the view.

Both before and after the delete/rollback the model has the following properties

isLoaded:true
isDirty: false
isSaving: false
isDeleted: false
isError: false
isNew: false
isValid: true

My template is as follows:

<table>
<thead>
  <tr>
    <th></th>
    <th>Name</th>
    <th>Number</th>
    <th>Speed Dial</th>
  </tr>
</thead>
<tbody>

  {{#each contact in model}}

    {{phonebook-row data=contact}}

  {{/each}}

</tbody>

</table>

Finally, here's the model hook in the respective route.

export default Ember.Route.extend({

  model: function() {
    return this.store.find('phonebook', { pageSize: 200 });
  }

});

I'm on Ember 1.11.1 and Ember Data 1.0.0-beta.16.1

Is this expected behaviour, or am I missing something? Any info would be greatly appreciated!

Upvotes: 2

Views: 280

Answers (1)

GJK
GJK

Reputation: 37389

Here's my best guess based on what I know about Ember Data:

Your model for this route is an array of records. Specifically, it's probably some subclass of RecordArray. The RecordArray likely handles a few things for you, like removing a record from the array when it's deleted. (Ember Data is known to do this elsewhere, like with FilteredRecordArray.) My guess is that Ember Data is smart enough to remove the record from the array when you delete it, but not smart enough to add it back to the array when you roll it back.

As for how to fix it, I'm not 100% sure. :/ After your model.rollback() call, maybe try calling model.reload(). It's not so much a solution as a workaround, but it could get you through temporarily. In the meantime, I'm going to try to reproduce this issue and see if it's a bug in Ember Data.

Upvotes: 1

Related Questions