Discussing the nuts and bolts of software development

Thursday, June 04, 2009

 

Using Mate to Dispatch Events with a Callback in ActionScript

Mate is a powerful event framework for Adobe Flex which provides advanced functionality for dispatching, listening for and handling events. The framework is tag-based, so the typical use case is to add tags provided by Mate to your MXML. This works very well in most situations, and Mate's documentation is generally very thorough and great for resolving any questions or issues that come up - until you have to do things without MXML.

I ran into a situation not too long ago where I had to dispatch an event with a callback from a service-like class. Since this wasn't a UI component, there was no corresponding MXML. I needed a pure-ActionScript solution, but still wanted to use Mate. In a matter of minutes, I had no problem dispatching my event*:

public class TestClass
{
public function generateEvent():void
{
var event:TestEvent= new TestEvent();
event.type = TestEvent.TYPE;

var dispatcher:Dispatcher = new Dispatcher();
dispatcher.generator = TestEvent;
dispatcher.dispatchEvent( event );
}
}

This was a good start, but I wanted to specify a method within the same service-like class to be called if the event returned successfully. Mate's dispatcher tag makes this very easy, but without any MXML, it was not an option. All the handler for the event does is generate a result or fault using Mate's ServiceResponseAnnouncer, so I needed a way to specify the callback from my dispatcher: just like the MXML tag allows, but using only ActionScript.

Looking through the docs didn't get me very far. The section on the Dispatcher provides information on using a dispatcher in ActionScript, but without a callback, and there is a section on using callbacks, but specific to the MXML implementation. I checked the ResponseHandler classes as well, and poked around a bit on Google without much luck.

Knowing that these properties exist, I attempted to piece it together myself (and eventually succeeded). In case anyone else ever runs into the same issue, and because I'd rather not go through all that searching/trial-and-error again, here is a working solution:

public class TestClass
{
public function generateEvent():void
{
var event:TestEvent= new TestEvent();
event.type = TestEvent.TYPE;

var handler:ResponseHandler = new ResponseHandler();
handler.type = ResponseEvent.RESULT;
handler.method = myCallback;

var dispatcher:Dispatcher = new Dispatcher();
dispatcher.generator = TestEvent;
dispatcher.responseHandlers = [handler];
dispatcher.dispatchEvent( event );
}

private function myCallback(event:ResponseEvent):void
{
trace( "callback reached!" );
}
}

One of the hardest parts to figure out was what method signature was required for the callback function. I found my answer in a comment in the ResponseHandler class - one more reason why open source frameworks and well commented code are the way to go!

*Aside: if a callback is not necessary, there's no need to use Mate. In fact, Mate's best practices specifically encourage using Flex's built-in dispatchEvent() method. Had I not needed a callback here, I could have called dispatchEvent() on the parent application (or any other DisplayObject within the scope of my service class).

This page is powered by Blogger. Isn't yours?