覆盖 Ext.data.Connection - 最佳实践 [英] Override Ext.data.Connection - Best Practice

查看:19
本文介绍了覆盖 Ext.data.Connection - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要覆盖 Ext.data.Connection 以显示登录表单.我目前在 Ext.application.launch 中执行此操作,它按预期工作.

I need to override Ext.data.Connection to show a Login Form. I do this at the moment in Ext.application.launch which works as expected.

是否可以将这段代码交换到不同的地方,比如在一个额外的文件中?

Is it possible to swap this piece of code somewhere different like in an extra file?

推荐答案

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

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

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

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

这篇关于覆盖 Ext.data.Connection - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆