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.
TweetRelated posts: