在ExtJS 4中调用服务器后取消store.remove

Mar*_*arc 9 extjs

我正在使用ExtJS 4并且有一个带有ajax代理和api的Ext.data.Store:

var gridStore = Ext.create('Ext.data.Store', {
    autoSync: true,
    proxy: {
        type: 'ajax',
        api: {
            read: 'myurl',
            create: 'myurl',
            update: 'myurl',
            destroy: 'myurl'
        },
        reader: {
             type: 'json',
             successProperty: 'success',
             root: 'data',
             messageProperty: 'message'
        },
        writer: {
             type: 'json',
             writeAllFields: false,
             root: 'data'
        },
        listeners: {
             exception: function(proxy, response, operation){
                 Ext.MessageBox.show({
                     title: 'Server error',
                     msg: operation.getError(),
                     icon: Ext.MessageBox.ERROR,
                     buttons: Ext.Msg.OK
                 });
             }
        }
    ...
Run Code Online (Sandbox Code Playgroud)

当我使用更新功能并且我的服务器返回一个json对象时success:false(因为他输错了),我关联网格中的字段仍然标记为已更改,并且用户可以选择更改其错误的值.

这很好.

但是当我从商店中删除记录时......

var store = Ext.StoreManager.lookup('gridStore');
store.remove(store.getById(id));
Run Code Online (Sandbox Code Playgroud)

...然后ExtJS首先从商店中删除此记录,然后调用ajax api.因此当destroy api返回时success:false,消息显示为异常,就像更新api一样,那很好,但我的记录已从商店中删除了!例如,来自服务器的异常表示您无法删除此记录,因为它已经在商店中删除了.

如何在服务器同步后取消存储删除?如果服务器返回,我希望记录留在商店中success:false.

任何的想法?也许是个bug?


更新解决方案

基于Ryan的anwer,我修改了异常监听器如下,这非常有效:

 listeners: {
     exception: function(proxy, response, operation){
         Ext.MessageBox.show(...);
         // get the removed records and insert them where they have been
         var removedRecords = gridStore.getRemovedRecords();
         for(var i=0; i<removedRecords.length; i++){
             var record = removedRecords[i];
             gridStore.insert(record.index, record);
         }
     }
 }
Run Code Online (Sandbox Code Playgroud)

小智 6

插入技术对我不起作用,删除的记录在下一次同步操作时保持标记为删除.我是Ext.data.Store.rejectChanges()为了这个目的而使用的.


Rya*_*yan 5

只需扩展您提供的代码,特别是listeners区域:

    listeners: {
         exception: function(proxy, response, operation){
             Ext.MessageBox.show({
                 title: 'Server error',
                 msg: operation.getError(),
                 icon: Ext.MessageBox.ERROR,
                 buttons: Ext.Msg.OK
             });
             gridStore.add(gridStore.getRemovedRecords());
         }
    }
Run Code Online (Sandbox Code Playgroud)