Override Ext.data.Connection - Best Practice

可紊 提交于 2019-11-30 09:57:20

If you need this only to recognize when user is not authenticated you might want to consider doing something else. Like adding a handler to Ajax singleton:

function addAjaxErrorHandler(object) {

    Ext.Ajax.on('requestexception', function(conn, response, options, e) {

        var statusCode = response.status,
        errorText = null,
        captionText = response.statusText;

        // 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) {

                // 401 - not authenticated. For some reason we don't have authentication cookie anymore
                if (r.ErrorCode == 401) {
                    Ext.MessageBox.alert('Error', 'You must log in to use application', 
                        function() { 
// do something when user is not authenticated
                        object);
                    return;
                }

                errorText = r.ErrorMessage;
            }

            if (errorText == null)
                errorText = response.responseText;
        }

        if (!captionText) 
            captionText = 'Error ' + statusCode;

        Ext.MessageBox.alert(captionText, errorText);
    },
    object);        
}

Then just call this function from your application.launch() function and pass application object so the scope if defined.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!