将事件侦听器附加到所有 URLRequest
我们有一个连接到处理身份验证的代理服务器的 Flex 应用程序。如果身份验证超时,代理服务器将返回 json 格式的错误字符串。我想做的是检查每个 URLRequest 响应并检查是否有错误消息并将其显示在 Flex 客户端中,然后重定向回登录屏幕。
所以我想知道是否可以以全局方式为所有 URLRequest 创建一个事件侦听器。无需搜索整个项目并向每个 URLRequest 添加一些方法。如果这可能的话有什么想法吗?
We have a flex application that connects to a proxy server which handles authentication. If the authentication has timeout out the proxy server returns a json formatted error string. What I would like to do is inspect every URLRequest response and check if there's an error message and display it in the flex client then redirect back to login screen.
So I'm wondering if its possible to create an event listener to all URLRequests in a global fashion. Without having to search through the project and add some method to each URLRequest. Any ideas if this is possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非您只使用一项服务,否则无法设置全局 URLRequest 处理程序。如果我是您,我会更多地考虑通过使用委托来正确构建您的应用程序,并始终通过整个应用程序中使用的特定服务来检查结果。
Unless you're only using one service, there is no way to set a global URLRequest handler. If I were you, I'd think more about architecting your application properly by using a delegate and always checking the result through a particular service which is used throughout the app.
J_A_X 有一些很好的建议,但我会更进一步。让我根据您提供的有限信息做出一些假设。
尽管您在 Adobe 示例中看到了新的服务生成代码,但直接从视图调用服务是非常糟糕的做法,部分原因是您所看到的问题 — 您最终可能会得到同一服务的大量副本代码散落在您的应用程序中。
根据您的应用程序的紧密程度(相信我,我继承了一些非常讨厌的东西,所以我知道这可能说起来容易做起来难),您可能会发现最简单的事情是删除所有这些不同的服务并替换通过让所有视图派发一个在顶层捕获的冒泡事件来实现它们。在顶层,您通过调用服务的一个实例来响应该事件,该实例再次在一个位置处理。
您可以选择也可以不选择将单个服务包装在委托中,但是一旦您以服务与视图分离的方式构建应用程序,您就可以随时做出该选择。
J_A_X has some good suggestions, but I'd take it a bit farther. Let me make some assumptions based on the limited information you've provided.
Despite what you see in the Adobe examples showing their new Service generation code, it's incredibly bad practice to call services directly from Views, in part because of the very problem you are seeing--you can wind up with lots of copies of the same service code littered all over your application.
Depending on how tightly interwoven your application is (believe me, I've inherited some pretty nasty stuff, so I know this might be easier said than done), you may find that the easiest thing is to remove all of those various services and replace them by having all your Views dispatch a bubbling event that gets caught at the top level. At the top level, you respond to that event by calling one instance of your service, which is again handled in one place.
You may or may not choose to wrap that single service in a delegate, but once you have your application archtected in a way where the service is decoupled from your Views, you can make that choice at any time.
您是否能够扩展该类并在对象的构造函数中添加事件侦听器?我不喜欢这种方法,但它可行。
您只需搜索/替换整个项目即可。
Would you be able to extend the class and add an event listener in the object's constructor? I don't like this approach but it could work.
You would just have to search/replace the whole project.