简介:

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的广播,并立即调用完成方法。
 
在框架源码中可以看到上述三个方法中的实现:
public function getNotification():INotification {
return notification;
   3: }
   4:  
/**
 * Adds a interceptor to the list of interceptors for the current notification.
 * 
 * @param interceptor The interceptor object to register.
 */
void {
this;            
  12:     interceptors.push(interceptor);
  13: }
  14:  
/**
 * Removes the specified interceptor from the list of interceptors.
 * 
 * @param interceptor The interceptor object to remove.
 */
void {
int = interceptors.indexOf(interceptor);
if (index >= 0) {
  23:         interceptors.splice(index, 1);
  24:         interceptor.dispose();
  25:     }
  26: }
  27:  
/**
 * Runs all interceptors registered with this NotificationProcessor.
 */
void {
int = interceptors.length;
  33:     var interceptor:IInterceptor;
int = 0; i < n; i++) {
  35:         interceptor = interceptors[i];
  36:         
  37:         interceptor.notification = notification;
  38:         interceptor.intercept();
  39:     }
  40: }
  41:  
/**
 * Sends a proceed event so that the notification can be send to the rest of
 * the PureMVC actors. Flags this instance as complete to ignore other interceptors.
 * Also sends a finish event to indicate that the processor can be disposed.
 * 
 * @param note The notification to proceed with. If null the current notification object is used.
 */
void {
if (!finished) {
new NotificationProcessorEvent(NotificationProcessorEvent.PROCEED, note));
  52:         finish();
  53:     }
  54: }
  55:  
/**
 * Sends an abort event. Flags this instance as complete to ignore other interceptors.
 * Also sends a finish event to indicate that the processor can be disposed.
 */
void {
if (!finished) {
new NotificationProcessorEvent(NotificationProcessorEvent.ABORT));
  63:         finish();
  64:     }
  65: }
  66:  
/**
 * If all interceptors have been skipped then sends a finish event to indicate
 * that the processor can be disposed.
 */
void {
if (!finished && ++skipCount == interceptors.length) {
  73:         finish();
  74:     }
  75: }
  76:  
/**
 * Flags the notification as finished and sends a finish event.
 */
void {
  81:     finished = true;
new NotificationProcessorEvent(NotificationProcessorEvent.FINISH));
  83: }

interceptor的处理过程是异步的,实例中对”save”这条消息进行了监听响应,也对“save”进行了拦截处理,但只有在点击“继续”按钮的时候才继续广播该notification。

 

英文原文(能力有限,本文翻译的可能有误,欢迎批评和斧正):http://code.google.com/p/fabrication/wiki/Interceptors#Requirements

示例demo的源码:http://code.google.com/p/fabrication/source/browse/examples/interceptor_demo/src#src%2Fmain%2Fflex%2Fcontroller%253Fstate%253Dclosed

 

本地查看效果实例:

 

如需要本示例程序的完整代码,立即下载>>

相关文章: