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

查看:112
本文介绍了覆盖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天全站免登陆