Recently I found out about the
[Event] meta tag in actionscript 3. I always
thought it was a Flex-only meta tag but that isn’t true.
Because many Flex components are coded in Actionscript only (for performance
purposes) you can also use the
[Event] meta tags in Actionscript only
projects. Since I am a big fan of code completion and strict typing
everything in my code I am certainly adding the following trick to that list.
Consider you have a custom (abstract) Button Class called
dispatches custom events like
cetera. All those events are declared in a custom class called
that extends the standard
MouseEvent Class and has extra properties
describing the button that fired the event.
UIButtonEvent Class I declared all static variables for the type names
of my Events:
public static const PRESS:String = ".onPress"; public static const RELEASE:String = ".onRelease"; public static const RELEASE_OUTSIDE:String = ".onReleaseOutside";
UIButton class I dispatch those Events:
dispatchEvent( new UIButtonEvent( UIButtonEvent.RELEASE_OUTSIDE ) );
Now in the same
UIButton Class, right below all the import statements and
before the actual class definition i put these lines:
The type attribute speaks for itself and points to the right package and Class where you have declared your custom Event class. Now the trick is the name attribute of the Event meta tag. This name needs to correspond with the static constant name declared in the UIButtonEvent class.
[Event(name="release")]corresponds with the static constant
[Event(name="releaseOutside")]corresponds with the static constant
[Event(name="addedToStage")]corresponds with the static constant
See how it works? Values in the name attribute of the Event meta tag should be
Camel Cased and start with a lower-case letter. Every Uppercase letter is
translated in to and underscore and thus creating static constant names like
Now picture some view in where you have an instance of UIButton and you want
to listen for the
var button:UIButton = new UIButton(); button.addEventListener( UIButtonEvent.RELEASE, onButtonRelease ); view.addChild( button );
In Flex Builder, as soon you type the last letters of the word “addEventListener” and then the bracket… a code completion window pops up. And because I used the Event meta tag in my UIButton Class, code completion now knows that the custom events from UIButtonEvent are associated with the UIButton and they will show up in code completion.
I started implementing this in all my custom components. Now when I am working on a project where I use some old component i build a few years ago, I instantly see what events I can listen for on that component and that tells me something about the workings of it without diving in to the actual code itself again.
Hope it helped (or will help) your workflow to.