问题
If I call destroyRecord and it fails on the server, it also disappears from the local store and from the UI. I need to somehow "rollback" if delete fails. I have tried something like this.
item.destroyRecord().then(function () {
Notify.success("item removed");
}).catch(function (response) {
//NEED TO ROLLBACK HERE - ANY IDEAS?
Notify.error('Failed to remove!');
});
回答1:
Firstly, rollback with relationships doesn't fully work in ember data, secondly the newer versions of ember data handle this better (ember data 1.0 beta 7+). Records have a rollback method on them for this very purpose, it's still in beta, but it does mostly what you're looking for.
item.destroyRecord().then(function () {
Notify.success("item removed");
}).catch(function (response) {
item.rollback();
Notify.error('Failed to remove!');
});
NOTE: In newer versions of Ember, item.rollback()
no longer functions, instead use item.rollbackAttributes()
as mentioned in comments from Marcelo.
来源:https://stackoverflow.com/questions/23937976/ember-data-delete-fails-how-to-rollback