My favorites | Sign in
Project Logo
                
Search
for
Updated Jan 09, 2009 by darshan.sawardekar
Labels: Featured
Interceptors  
Using Interceptors


Introduction

Interceptors provide means to alter the normal flow of PureMVC Notifications before they reach Commands and Mediators. With Interceptors you can,

Requirements

This feature needs Fabrication version 0.6 or above.

Registering Interceptors

Interceptors are registered using the registerInterceptor method. This method is available on the FabricationFacade or within subclasses of SimpleFabricationCommand.

The syntax is,

registerInterceptor(noteName:String, clazz:Class, parameters:Object = null)
// 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"});

Implementing Interceptors

Interceptors need to extend the AbstractInterceptor class and implement a concrete intercept method. The intercept method will be called when the notification that it was registered for is fired anywhere in the application. The interceptor instance has access to the following properties at the time the intercept method is called,

Once the intercept method is called the notification will not reach the rest of the PureMVC actors until you call one of the following methods.

The interceptor processing can be asynchronous. In the following example an alert is show when the notification is intercepted. If Ok was clicked in the alert the notification is allowed to proceed otherwise it is aborted.

override public function intercept():void {
	Alert.yesLabel = "Ok";
	Alert.cancelLabel = "Cancel";
	Alert.show("Are you sure?", "Confirm", Alert.OK | Alert.CANCEL, null, closeListener);
}

private function closeListener(event:CloseEvent):void {
	if (event.detail == Alert.OK) {
		proceed();
	} else {
		abort();
	}
}

Examples

  1. Simple Interceptor Browse] SVN]
  2. StateMachine utility with Fabrication Browse] SVN]

Comment by da.swid, Mar 28, 2009

Check out aspect oriented programming if not familiar with it already. Great utility!


Sign in to add a comment
Hosted by Google Code