问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/9682249/override-ext-data-connection-best-practice