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

5

我需要重写 Ext.data.Connection 来显示登录表单。目前我在 Ext.application.launch 中实现了这个功能,效果如预期。

请问是否可以将这段代码放到其他地方,比如一个额外的文件中?


你为什么想要重写Connection以便在用户未经过身份验证时显示登录表单,以向用户呈现登录表单? - sha
没错。而且如果会话超时的话。 - Julian Hollmann
我为Ajax错误添加了全局处理程序,并在那里分析了非身份验证错误。也许这是适合您的东西。 - sha
你能给我展示一个例子吗?我感到有点被遗弃,因为缺乏例子。 - Julian Hollmann
这就是翻译的结果。如果你觉得可以,请告诉我。 - sha
1个回答

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);        
}

然后只需从您的 application.launch() 函数调用此函数,并传递应用程序对象,以便定义作用域。

非常感谢,这不完全是我需要的,但它指引了我正确的方向。 :) - Julian Hollmann
如果在其他的Ajax请求调用中有失败处理程序,那么看起来这段代码不会被执行。 - jujule

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接