Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Ext.data.Connection - Best Practice

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?

like image 909
Julian Hollmann Avatar asked Mar 13 '12 10:03

Julian Hollmann


1 Answers

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.

like image 188
sha Avatar answered Oct 26 '22 23:10

sha