在 Extjs 中显示错误消息的最佳方法是什么

what could be the best way to show error message in Extjs?

本文关键字:最佳 方法 是什么 消息 错误 Extjs 显示      更新时间:2023-09-26

我正在使用store.synch()方法来发布数据。并从服务器端完成验证,目前我正在使用消息框显示错误消息。现在我想用一些不同的方式来显示错误,但不是 markInvalid(),因为为此我还必须更改每个 js fiels和 api。所以有没有替代的markInvalid()?

Extjs 数据存储提供了侦听器,其中一个侦听器是例外(适用于 ExtJs <3.x)

这是代码。

listeners: { //Exception Handler for the Ajax Request
    exception: function(proxy, response, operation){
        var error = Ext.decode(response.responseText);
        Ext.MessageBox.show({
            title: 'REMOTE EXCEPTION',
            msg: error.message,
            icon: Ext.MessageBox.ERROR,
            buttons: Ext.Msg.OK
        });
    }
}

顺便说一句,我没有得到什么是 markInvalid()

嗨,

Naresh Tank,我解决您的问题是监视所有 ajax 请求。通过这种方式,您可以发送您想要的错误消息,无论它来自商店还是表单。

我希望这有所帮助。

在应用程序上.js

init: function() {
    this.addAjaxErrorHandler(this);
},
addAjaxErrorHandler: function(object) {
    Ext.Ajax.on('requestexception', function(conn, response, options, e) {
        var statusCode = response.status,
            errorText = null,
            captionText = response.statusText;
        if (statusCode === 0 || statusCode === 401) {
            Ext.Ajax.abortAll();
        }
        if(response.statusText==="Authorization Required"){
                Ext.Ajax.abortAll();
        }
        // 404 - file or method not found - special case
        if (statusCode == 404) {
            Ext.MessageBox.alert('Error 404', 'URL ' + response.request.options.url + ' not found');
            return;
        }
        if (response.responseText !== undefined) {
            var r = Ext.decode(response.responseText, true);
            if (r !== null) {
                errorText = r.ErrorMessage;
            }
            if (errorText === null)
                errorText = response.responseText;
        }
        if (!captionText)
            captionText = 'Error ' + statusCode;
        Ext.MessageBox.alert(captionText, errorText);
    },
                object);
    Ext.Ajax.on('requestcomplete', function(conn, response, options, e) {
        var statusCode = response.status,
            errorText = null,
            captionText = response.statusText;
        if (response.responseText !== undefined) {
            var r = Ext.decode(response.responseText, true);
            if (r !== null && r.success === false) {
                try{
                    if(typeof r.data[0].idUsr !== 'undefined')
                        return;
                }catch(e){}
                errorText = r.msg;
                if (errorText === null)
                    errorText = response.responseText;
                if (!captionText)
                    captionText = 'Error ' + statusCode;
                Ext.MessageBox.alert(captionText, errorText);
            }
        }
    },
                object);
};