javascript EXTJS 4 - 全局异常监听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8008489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EXTJS 4 - Global exception listener
提问by user798612
I have a situation where I am making ajax
requests to a server from various Ext gridpanel
etc. In an Admin area.
我有一种情况,我ajax
从各种分机gridpanel
等向服务器发出请求。在管理区域。
The logged in user will be logged out if there is no activity for eg. 5 minutes which is normal.
如果没有活动,例如,登录的用户将被注销。5分钟是正常的。
In this case the server sends back a redirect 403
to the login page.
在这种情况下,服务器会发送回403
登录页面的重定向。
Right now I am inserting a:
现在我正在插入一个:
listeners: {
exception: function(proxy, response, operation, eOpts) {
if (response.status == '403')
window.location = 'login';
}
}
To every store's
proxy which is a little overkill.
对于每个store's
代理来说,这有点矫枉过正。
Could someone be kind enough and let me know how I could add a listener to all communications between ExtJS and server?
有人可以友好地告诉我如何为 ExtJS 和服务器之间的所有通信添加侦听器吗?
I am using the MVC Application Architecture so it could probably be a one liner in the controller.js
or app.js
.
我正在使用 MVC 应用程序架构,因此它可能是controller.js
或app.js
.
Thanks
谢谢
回答by Sascha
In the beginning of your app insert the following snippet. With this EVERYresponse, whether it's from a store or a form or ..., will be checked and redirect to login page.
在您的应用程序的开头插入以下代码段。有了这个每一个响应,无论是来自商店、表单还是......,都将被检查并重定向到登录页面。
Ext.Ajax.on('requestexception', function (conn, response, options) {
if (response.status === 403) {
window.location = 'login';
}
});
回答by nightwatch
I'm not really sure if this will catch all ajax requests but assuming you're using AjaxProxy for all communication with the server it should work: handle the 'requestexception' event in the Ext.Ajax singleton something like this
我不确定这是否会捕获所有 ajax 请求,但假设您使用 AjaxProxy 与服务器进行所有通信,它应该可以工作:处理 Ext.Ajax 单例中的“requestexception”事件,如下所示
Ext.Ajax.on('requestexception', function(conn, response, options, eOpts) {
//your error handling here
});
I haven't tried it but if you do, could you post an update here?
我还没有尝试过,但如果你尝试过,你能在这里发布更新吗?
回答by Carlos Jaime C. De Leon
A more complete solution, wherein it will be a catch-all is this:
一个更完整的解决方案,其中它将是一个包罗万象的:
Ext.util.Observable.observe(Ext.data.Connection, {
requestexception: function(conn, response, options) {
if(response.status == '403')
window.location = 'login';
}
});
This is because the underlying class, Ext.data.Connection
is used not only in Ext.Ajax
but as well as the Ext.data.Proxy
that is used by Ext.data.Store, Ext.data.Model
. This handles exceptions on such calls as store.load()
and model.save()
. This should be a more complete catch-all handler.
这是因为基础类,Ext.data.Connection
不仅用于 ,Ext.Ajax
而且还Ext.data.Proxy
用于Ext.data.Store, Ext.data.Model
. 这会处理诸如store.load()
and 之类的调用的异常model.save()
。这应该是一个更完整的包罗万象的处理程序。
See more details in my blog post.