Getting Code Completion on Custom Events in Flex with the Event Metadata Tag

January 10th, 2009 by ryanstewart

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

6 Responses

  1. ilja.panin

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

  2. Matt

    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
    { . . . }

  3. Nate Beck

    I love [Inspectable] as well. The enumeration property is such a life saver when you’re dealing with ENUMS from a database.

  4. ryanstewart

    Whoops, thanks Ilja, I was messing with different values and copy and pasted the wrong thing.

  5. Tink

    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")]

  6. Tink

    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.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

About Ryan Stewart – Rich Internet Application Mountaineer

A blog by a Platform Evangelist at Adobe covering Adobe's RIA platform. Includes posts about Adobe Flex, Adobe AIR, ColdFusion, LiveCycle, Thermo, and everything in between.