PDA

View Full Version : How to revert destroy when server return a failure



julien.meyer
25 Jan 2012, 6:57 AM
Hello,

When the server response to a delete operation (from a model instance destroy call) is a failure (success to false), the store doesn't revert the operation and the instance is nevertheless deleted from the application.
I looked into extjs source files and it seems that the model instance is first removed from the store and after, the proxy is called. The proxy result is just used for user callback function. Is that correct or Have i bad understand some thing ?

If my analysis is correct, how to implement a "rollback" operation ?

Thanks

Farish
25 Jan 2012, 8:59 AM
You may use the following override:


Ext.override(Ext.data.Store, {
rejectChanges: function() {
var me = this;

// re-add removed records
Ext.each(me.removed, function(rec) {
rec.join(me);
me.data.add(rec);
});
me.removed = [];

// revert dirty records and trash newly added records ('phantoms')
me.each(function(rec) {
if (rec.dirty) {
rec.reject(true);
}
if (rec.phantom) {
me.data.remove(rec);
}
});
}
});

On failure, you can call:

store.rejectChanges();

If you just want to do it for delete, then you can remove if conditions. The deleted records are stored in the "removed" array in the store.

Regards,
Farish

julien.meyer
26 Jan 2012, 12:47 AM
Thanks Farish,

Your solution can "rollback" a delete operation (or update) but it add records at the end of the store and not the previous position.
And I think that if the store is a TreeStore, it doesn't work.

Farish
26 Jan 2012, 1:55 AM
i havent tried this with a tree store. If you want to reinsert the deleted records at their original position, I dont know any short way for that. What you could do is to push the index and id of the record to be removed into another array before calling store.remove. The when you want to rollback, use insert instead of add and use the position from that array for insert. OR if your records are sorted, you can add them at the end and just call store.sort(fieldname, direction).