Getting Code Completion on Custom Events in Flex with the Event Metadata Tag
I was working on an application today and noticed that one of my classes wasn’t giving me code completion on the events when I called the addEventListener method. The reason is pretty simple but I figured blogging it would help keep it in my mind. Plus, I’ve never been as solid on the metadata tags in Flex as I should be.
In order to create the custom event I created a class which extends EventDispatcher. This allows me to call the dispatchEvent method for the class and bubble that up to the application. So I create a new variable and instantiate it with the constructor, add an event listener for my custom event, then call the method which ultimately dispatches the event:
var str:String = fr.data.readUTFBytes(fr.size); var xml:XML = new XML(str); gpx.addEventListener(ParseEvent.PARSE_COMPLETE,onGpxParseComplete); gpx.load(xml);
That’s pretty standard. But when I typed addEventListener and went to create a method to handle the event, I just got the default events for the EventDispatcher class, activate and deactivate.
In order to get code completion, you have to add the Event metadata tag to the class that is extending EventDispatcher. The Event metadata tag takes two parameters, the name and type. The name attribute just refers to the name of the event, in my case, the value of my static const. Type is the name of the event class. So for my ParseEvent with a PARSE_COMPLETE event name, I just add the following metadata to the class that extends EventDispatcher:
[Event(name="parseComplete", type="com.adobe.gpslib.gpx.events.ParseEvent")]
Metadata is a powerful part of the Flex framework and is something well worth looking into.
Posted in Flex







January 10th, 2009 at 8:27 am
You have error in the metatag
If you wont to get code completion as
ParseEvent.PARSE_COMPLETE
you must add metadata
[Event(name="parseComplete", type="com.adobe.gpslib.gpx.events.ParseEvent")]
I wrote about in my blog (on russian
, but you can translate by google
http://translate.google.com/translate?prev=&hl=ru&ie=UTF-8&u=http%3A%2F%2Fthe33cows.com%2Factionscript-30%2F2008%2F01%2F23%2F33%2F&sl=ru&tl=en&history_state0=)
January 10th, 2009 at 10:23 am
I’ve been loving [Inspectable]
Makes me feel warm and fuzzy when another developer gets code hinting on a custom component.
[Inspectable(category="General", enumeration="DIGITAL, BACK, COUNTRY", defaultValue="DIGITAL")]
public function set blogName(value:String):void
{ . . . }
January 10th, 2009 at 11:28 am
I love [Inspectable] as well. The enumeration property is such a life saver when you’re dealing with ENUMS from a database.
January 10th, 2009 at 12:36 pm
Whoops, thanks Ilja, I was messing with different values and copy and pasted the wrong thing.
January 10th, 2009 at 4:04 pm
Constants, type and error checking in metadata would be a touch. Come on Adobe! FB could also write this metadata by itself after it see’s you dispatching an event? Typing a couple of strings is a bit naff.
I also think the naming convention used in the metadata doesn’t tie in very well with the naming used in ActionScript.
[Event(name="eventName", type="package.eventType")]
so here the string is ‘name; and the class is ‘type’.
Event(type:String, bubbles:Boolean = false, cancelable:Boolean = false)
yet in the above AS, the string is now referred to as ‘type’.
Wouldn’t the following be clearer…?
[Event(type="eventType", class="package.eventClass")]
January 12th, 2009 at 11:52 am
To add to that, if FB adds this metadata, so all events are covered, its feasible that it could show a warning if a listener is added to a component that doesn’t dispatch the event (warning only as it could be an event that bubbles).
That would really tighten up apps and frameworks like Cairngorm.