2012-03-13 72 views
5

我需要重写Ext.data.Connection才能显示登录表单。 我现在在Ext.application.launch中按预期工作。重写Ext.data.Connection - 最佳实践

是否有可能交换这段代码在不同的地方像一个额外的文件?

+0

为什么你想重写连接以准确显示登录表单?如果他未通过身份验证,向用户显示登录表单? – sha 2012-03-13 10:32:20

+0

正是。而且如果会话超时。 – 2012-03-13 10:34:20

+0

我为Ajax错误添加了全局处理程序,并在那里分析了未验证的错误。也许这也适合你。 – sha 2012-03-13 11:04:26

回答

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()函数调用这个函数,并通过应用程序对象,以便范围,如果定义。

+0

非常感谢,这不完全是我所需要的,但它指出了我正确的方向。 :) – 2012-03-13 11:56:41

+0

很高兴帮助:) – sha 2012-03-13 11:59:09

+0

看起来像这样不会被执行,如果你有一个失败处理程序在别人ajax请求调用。 – jujule 2012-10-30 10:45:31