ExtJS 4.2.1 - 从服务器收到错误后拒绝存储中的更改

Posted

技术标签:

【中文标题】ExtJS 4.2.1 - 从服务器收到错误后拒绝存储中的更改【英文标题】:ExtJS 4.2.1 - reject changes in store after getting error from server 【发布时间】:2014-04-09 15:21:10 【问题描述】:

在我的应用程序中,我正在使用以下代码删除记录:

//try to delete item
s.eventStore.remove(s.ctx.rec);
//error handling
s.eventStore.getProxy().on(
    exception: function() 
        s.eventStore.rejectChanges();
        Ext.MessageBox.show(
            title: 'Server error',
            msg: 'data restored',
            icon: Ext.MessageBox.ERROR,
            buttons: Ext.Msg.OK
        );
    
);

这很好,但我想避免在每个地方都添加侦听器,而是想在我的商店中添加一个侦听器。 我试过这样修改我的商店:

Ext.define('Urlopy.Store.Holidays', 
    extend: "Sch.data.EventStore",
    autoLoad: false,//zmiana
    autoSync: true,
    batch: false,
    proxy: 
        type: 'rest',
        pageParam: false, //to remove param "page"
        startParam: false, //to remove param "start"
        limitParam: false, //to remove param "limit"
        noCache: false, //to remove param "_dc"
        url: window.appUrl + 'api/Holidays',
        reader: 
            type: 'json',
            root: 'data'
        ,
        writer: 
            type: 'json'
        ,
        listeners: 
            exception: function (store, response, operation) 
                    var data = Ext.decode(response.responseText);
                    Ext.MessageBox.show(
                        title: 'Server error',
                        msg: data.Message,
                        icon: Ext.MessageBox.ERROR,
                        buttons: Ext.Msg.OK
                    );
                    store.rejectChanges();
            
        
    ,
    model: 'Urlopy.Model.Holiday'
);

这基本上是同一个监听器,但是当我尝试拒绝更改时,我得到了这个错误:

Uncaught TypeError: Object [object Object] has no method 'rejectChanges'

如果服务器出现问题,我如何修改我的商店以进行全局异常处理和恢复事件?

我不想使用Cancel store.remove after server call in ExtJS 4 (store.add(store.getRemovedRecords());) 的解决方案

编辑:

我已将我的stode 定义更新为:

Ext.define('Urlopy.Store.Holidays', 
    extend: "Sch.data.EventStore",
    autoLoad: false,
    autoSync: true,
    batch: false,
    proxy: 
        type: 'rest',
        pageParam: false, //to remove param "page"
        startParam: false, //to remove param "start"
        limitParam: false, //to remove param "limit"
        noCache: false, //to remove param "_dc"
        url: window.appUrl + 'api/Holidays',
        reader: 
            type: 'json',
            root: 'data'
        ,
        writer: 
            type: 'json'
        ,
        listeners: 
            exception: function(proxy, response, operation) 

                console.log(this);
                this.rejectChanges();
            ,
            scope: this
        
    ,
    model: 'Urlopy.Model.Holiday'
);

但我仍然得到相同的错误并且控制台输出 Window 对象而不是存储。

【问题讨论】:

【参考方案1】:

rejecting 每个record of the operation 怎么样?

listeners: 
  exception: function(proxy, request, operation, eOpts)
    // ... any user notification ...

    Ext.each(operation.getRecords(), function(record)
      record.reject();
    );
  

PS:这对 ExtJS 5.x 也有效(基本模型内部通常可用于整个应用程序)!

【讨论】:

【参考方案2】:

这应该从控制器解决。如果对同步方法的调用失败,它将触发失败回调,您可以在其中拒绝更改。

store.sync(
  failure : function()
     store.rejectChanges();
  
);

【讨论】:

【参考方案3】:

异常事件的签名是:

exception( this, response, operation, eOpts )

其中this 是代理。你需要添加一个范围:

listeners: 
    exception: function (proxy, response, operation) 
        var data = Ext.decode(response.responseText);
        Ext.MessageBox.show(
            title: 'Server error',
            msg: data.Message,
            icon: Ext.MessageBox.ERROR,
            buttons: Ext.Msg.OK
        );
        this.rejectChanges();
    ,
    scope: this

它应该可以工作,this 应该是商店。

【讨论】:

不幸的是我不工作。我按照您的建议更改了侦听器,但仍然遇到相同的异常。我尝试在this.rejectChanges(); 之前添加console.log(this);,在控制台中我得到'Window' 而不是store。 我在代理中添加了监听器,也许这与这个错误有关? 只有代理有异常事件。所以它是正确的地方。尝试登录operation也许有对商店的引用。 不幸的是它不存在。我检查了proxyresponseoperation。一定有办法做到这一点。 好吧,我放弃了,你可以给 store 一个 ID 并将这个 ID 设置为代理的额外配置参数,然后使用Ext.getStore() 来获取 store...【参考方案4】:

新想法:

您可以直接销毁记录,model.destroy() 有一个operation 参数,您可以在其中添加一个failure 函数。

【讨论】:

【参考方案5】:

在您的商店代理中为异常添加一个侦听器。下面是一个示例,它在异常时获取响应消息并填充弹出窗口并拒绝更改。

     listeners: 
             exception: function (proxy, response, operation, eOpts)
                 var data = Ext.decode(response.responseText);
                 Ext.MessageBox.alert('Error', 'The proxy event failed: <br><li>'+data.errorMessage+'</li>');
                 Ext.each(operation.getRecords(), function(record)
                     record.reject();
                 );
             
         

【讨论】:

以上是关于ExtJS 4.2.1 - 从服务器收到错误后拒绝存储中的更改的主要内容,如果未能解决你的问题,请参考以下文章

当我启动 apache MySQL 服务器时,我收到错误“(HY000/2002):无法建立连接,因为目标机器主动拒绝它。”

如何检查从服务器收到的响应是 html 还是 json 并在 extjs 中按名称查找 html 表单?

Heroku 上的 Redis 连接被拒绝

从应用程序访问适配器时连接被拒绝错误

为啥我收到 Spring Security 和 Keycloak 的“访问被拒绝”错误?

持久化和拒绝对层次关联的不同级别的更改 - Extjs 5/Sencha