fabrication的拦截器Interceptors简介
简介:
Interceptors(拦截器),主要目的是为了改变PureMVC的消息通知在到达Commands和Mediators的正常执行顺序。 在拦截器里可以:
·废弃notification不再向外广播
·修改notificationg再向外广播
·使用新的notification替换原有的notification
·无限制发送这一次notification
·Interceptors与commands类似,可以使用PureMVC实例访问和修改应用程序
依赖:
Fabrication V0.6+ 可使用此功能
Interceptors的注册
Interceptors使用registerInterceptor方法来进行的注册,该方法可在FabricationFacade或是SimpleFabricationCommand的子类中使用。
语法,registerInterceptor(noteName:String, clazz:Class, parameters:Object=null)
noteName:注册notification它的名称
clazz:拦截器的处理类
parameters:可选参数,供拦截器处理类使用
// registers the interceptor for the save notification name.
registerInterceptor("save", MyInterceptor);
// registers the interceptor with save notification with extra parameters
registerInterceptor("save", MyInterceptor, {foo:"bar"});
Interceptors的实现
拦截类的处理类(前面提及的clazz),必须继承自AbstractInterceptor类,并实现intercept方法。
当注册的Notifications被发送时(使用sendNotification或是routerNotification方法进行发送),
拦截器会调用该处理类的intercept方法,这些方法中可以直接使用的对象有:
notification:被拦截的notification对象
parameters:拦截器注册时传入的可选参数
processor:对外提供的几种方法,忽略、跳过或中止该notification的广播,包含三个方法<proceed|abort|skip>
proceed:允许notification继续进行广播,可以使用新的notification替换当前的notification。
skip:如果所有对该notification的拦截器处理已经完成,就直接调用完成方法。不然就再调用该notification的其它拦截器实例
abort:直接中止该notification的广播,并立即调用完成方法。
在框架源码中可以看到上述三个方法中的实现:
1: public function getNotification():INotification {
2: return notification;
3: }
4:
5: /**
6: * Adds a interceptor to the list of interceptors for the current notification.
7: *
8: * @param interceptor The interceptor object to register.
9: */
10: public function addInterceptor(interceptor:IInterceptor):void {
11: interceptor.processor = this;
12: interceptors.push(interceptor);
13: }
14:
15: /**
16: * Removes the specified interceptor from the list of interceptors.
17: *
18: * @param interceptor The interceptor object to remove.
19: */
20: public function removeInterceptor(interceptor:IInterceptor):void {
21: var index:int = interceptors.indexOf(interceptor);
22: if (index >= 0) {
23: interceptors.splice(index, 1);
24: interceptor.dispose();
25: }
26: }
27:
28: /**
29: * Runs all interceptors registered with this NotificationProcessor.
30: */
31: public function run():void {
32: var n:int = interceptors.length;
33: var interceptor:IInterceptor;
34: for (var i:int = 0; i < n; i++) {
35: interceptor = interceptors[i];
36:
37: interceptor.notification = notification;
38: interceptor.intercept();
39: }
40: }
41:
42: /**
43: * Sends a proceed event so that the notification can be send to the rest of
44: * the PureMVC actors. Flags this instance as complete to ignore other interceptors.
45: * Also sends a finish event to indicate that the processor can be disposed.
46: *
47: * @param note The notification to proceed with. If null the current notification object is used.
48: */
49: public function proceed(note:INotification = null):void {
50: if (!finished) {
51: dispatchEvent(new NotificationProcessorEvent(NotificationProcessorEvent.PROCEED, note));
52: finish();
53: }
54: }
55:
56: /**
57: * Sends an abort event. Flags this instance as complete to ignore other interceptors.
58: * Also sends a finish event to indicate that the processor can be disposed.
59: */
60: public function abort():void {
61: if (!finished) {
62: dispatchEvent(new NotificationProcessorEvent(NotificationProcessorEvent.ABORT));
63: finish();
64: }
65: }
66:
67: /**
68: * If all interceptors have been skipped then sends a finish event to indicate
69: * that the processor can be disposed.
70: */
71: public function skip():void {
72: if (!finished && ++skipCount == interceptors.length) {
73: finish();
74: }
75: }
76:
77: /**
78: * Flags the notification as finished and sends a finish event.
79: */
80: public function finish():void {
81: finished = true;
82: dispatchEvent(new NotificationProcessorEvent(NotificationProcessorEvent.FINISH));
83: }
interceptor的处理过程是异步的,实例中对”save”这条消息进行了监听响应,也对“save”进行了拦截处理,但只有在点击“继续”按钮的时候才继续广播该notification。
英文原文(能力有限,本文翻译的可能有误,欢迎批评和斧正):http://code.google.com/p/fabrication/wiki/Interceptors#Requirements
本地查看效果实例:
如需要本示例程序的完整代码,立即下载>>