Profile
Minimize
The ExoBlog is maintained by John T. Stough... read the profile here: View John Stough's profile on LinkedIn
Mr. Stough is a member of (ISC)2 as a Certified Information Systems Security Professional: Certified Information Systems Security Professional
Search
Minimize
  
Topics
Minimize
[Hint: click the arrow beside a blog to see sub-categories]
  
Custom Events
Minimize
Location: BlogsExoBlog (by John)FLEXocubic (Flex & AS3)    
Posted by: exocubic 4/5/2008 2:22 PM
I recently encountered a need to build a custom event in Flex 3.  I've done this plenty of times in other languages, but my first time in Flex was a bit more frustrating than I expected; there are a few oddities about how to declare the event name and ensuring it gets correctly called from the client object, etc. that I think would throw off a new developer, so I decieded to look for other examples people had posted on this kind of thing and maybe upload my own demo of the functionality.

One good tutorial I found was this one:
http://flexblog.faratasystems.com/?p=170

There are some decent notes in the Flex documentation, but they are pretty generic.  Of course, you need to understand your reason for creating the event or else you are better off using a pre-defined event - this is the point the documentation makes; so, assuming you know you want a truly custom event

The Problem:

The situation where this came up for me is in developing a simple component as a pop-up control.  It basically has several selections the user has to make (start & end date, a few other things) then go away.  No big deal, except I want to have a cancel button on the control, so you can look for a built-in "hide" event, nor (because good components should be encapsulated) should you just expose the click event of the cancel or "set"/"ok"/"whatever" button.  I wanted my event only to fire when the right conditions exist and I want the event to carry with it the data it needs (like the start & end date) that were requested.

I built the custom pop-up and the seperate event object (a *.mxml file and a *.as file, respectively) and everything compiled.  My dialog worked great (IMHO) and no errors were generated on the dispatch event call:
var theEvent:DateSelected = new DateSelected('dateSelected');
theEvent.startDate = _startDate;
theEvent.endDate = _endDate;
dispatchEvent(theEvent);
...but the event handler never got called!

The Magic:

It appears that the solution was (of course) far more simple than I imagined.  The magic is in the declaration of the event meta tag - it has to be a certain way, but the documentation is a bit confusing on this.  When you read the documentation, they encourage you to use a predefined type and talk about "the problem" of using strings instead of constants in the event listeners, etc.  So, since there was not a specific explaination or example on creating a non-predefined event type, I used this as my Event meta data declaration:
[Event(name="dateSelected", type="com.events.DateSelected")]

The rest of the event you create is really simple - it is just an object with custom properties, so if you give it public properties and data, then when the event is called, it just takes itself along, data and all, but you need to listen for it, so this way my handler.  Thanks to the confusion about strings and the fact that my event listener was never getting called (after trying SEVERAL different options), this was my initial event listener:
dateSelection.addEventListener("dateSelected", onDateSelection, false, 0 , true);   //dateSelection is the custom popup dialog...
(this is the correct event listener, by the way)

I figured, based on the warning in the Adobe docs, that the problem had to be in the addEventListener settings, focusing on the string (because it was not a predefined standard event type).  I tried "com.events.DateSelected" and "com.events.DateSelected.dateSelected" and a few other things and then just got really mad.  When I calmed down, I remembered that the meta tag had some strings also.

Here is my new (properly working) meta tag:
[Event(name="dateSelected", type="flash.events.Event")]

As usual, I overthought the problem. The documentation says you can give it a name, but it never says you should or even can mess with the second string.  My bad assumption (and n00bie status as a Flex developer) that I had to define the type as a custom string as well made this event dispatch call to into cyber-space.

Lesson

So, lessons in overthinking things and trying to go too-far too-fast in customization.  I should have tried to overide a predefined event and then only change the name - I'd have been done in half the time.  But I tried a shortcut and wasted time.  Hope this helps someone else trying to create a custom event handler!
 
(...more to come if I get around to the demo)
Permalink |  Trackback
Think Outside the Blog
Minimize
From time to time, we all have random thoughts... sometimes they are profound, sometimes they are not, but if they are worth keeping then they are worth recording.  This blog is used to record the thoughts on what it means the think creatively, solve problems, and improve processes.  You have to be a registered user of the site in order to comment; help us maintain the integrity of the site and comments posted on it.  We would love to hear your feedback, so please take a moment to register.
  
 
 
©   2008 Exocubic, LLC      Terms Of Use     Privacy Statement