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).

Comments:
In my opinion, education means a lot these days. On https://dissertationowl.com/ you can find the best dissertation writing services if you need.
 


شركة نقل اثاث من الرياض الى حائل شركة نقل اثاث من الرياض الى حائل


معلم دهانات بالرياض معلم دهانات بالرياض

فني تركيب ورق جدران بالرياض فني تركيب ورق جدران بالرياض


 
Thanks for sharing this.,
Leanpitch provides online training in CSPO during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSPO Certification Workshop in different cities.

CSPO certification

Product owner certification


 
Thanks for sharing this.,
Leanpitch provides online training in CSPO during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSPO Certification Workshop in different cities.
CSPO certification online

Certified scrum product owner

 
Thanks for sharing this.,
Leanpitch provides online training in CSPO during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSPO Certification Workshop in different cities
CSPO online certification

CSPO certification cost
 
Thanks for sharing this.,
Leanpitch provides online training in CSPO during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSPO Certification Workshop in different cities
CSPO TRAINING ONLINE

CSPO ONLINE TRAINING
 
Thanks for sharing this.,
Leanpitch provides online training in CSPO during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSPO Certification Workshop in different cities
CSPO TRAINING

CSPO ONLINE

 
Thank a lot. You have done excellent job. I enjoyed your blog . Nice efforts
Data Science Certification in Hyderabad
 
Great to become visiting your weblog once more, it has been a very long time for me. Pleasantly this article i've been sat tight for such a long time. I will require this post to add up to my task in the school, and it has identical subject along with your review. Much appreciated, great offer. data science course in nagpur
 
I was basically inspecting through the web filtering for certain data and ran over your blog. I am flabbergasted by the data that you have on this blog. It shows how well you welcome this subject. Bookmarked this page, will return for extra. data science course in jaipur
 
Thanks for the informative and helpful post, obviously in your blog everything is good..
business analytics course

 
Post a Comment



<< Home

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