覆盖Ext.data.Connection - 最佳实践

Jul*_*ann 5 javascript extjs extjs4 extjs-mvc

我需要覆盖Ext.data.Connection以显示登录表单.我现在在Ext.application.launch中这样做,它按预期工作.

是否有可能将这段代码交换到不同的额外文件中?

sha*_*sha 6

如果您只需要识别用户未经过身份验证的时间,您可能需要考虑做其他事情.就像向Ajax单例添加处理程序一样:

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);        
}
Run Code Online (Sandbox Code Playgroud)

然后从application.launch()函数中调用此函数并传递应用程序对象,以便定义范围.