Using Your Own Custom Metadata in AS3

Flex 3 gives us a great new feature, custom metadata tags. Now, I know you could actually use custom metadata in Flex 2, but you would have to add “-keep-as3-metadata MyTag” to every single project that utilized these custom tags. In Flex 3, if you add “-keep-as3-metadata MyTag” to a library (using compc to compile a SWC or a Flex Builder Library project), then EVERY project that uses that SWC will automatically keep the “MyTag” metadata tags. This allows custom libraries that utilize these tags for development.

Would be cool to create a library to hook up listeners so you can create listeners like this:

[Listen(obj="this.closeButton", event="click")]
public function closeClickHandler(event:MouseEvent) {...}

You’d use -keep-as3-metadata Listen in the libraries compiler options. Maybe if you want I could post a tutorial on doing something like this. Drop me note and let me know if there is interest.

14 Responses to “Using Your Own Custom Metadata in AS3”

  1. edison Says:

    i really want to know how to define metadata tags and how to use them. it’s better to provide me a tutorial.
    thanks a lot~

  2. Alvaro Says:

    Hi, nice blog, will be cool if you publish this tutorial

  3. peper Says:

    I worked with metadata lately. There’s a trap in Your idea. You won’t se a compiler error if there’s no this.closeButton object or ‘click’ event.
    It’s not much more code in

    this.closeButton.addEventListener(MouseEvent.CLICK, closeClockHandler);

    and everything is checked in compile time, and I use constant, not string value for event name.

  4. Jacob Wright Says:

    Very true. There’s trade-offs to using metadata for sure.

  5. Bob Calco Says:

    AS3 Metadata tags is one of those topics a few people write about, but nobody goes all the way. They invariably mention the -keep-as3-metadata option, and invariably punt when it comes to showing how to use it in action. It drives me bonkers because I love Java annotations and .NET attributes and would like somebody to provide some concrete comparisons between them. Annotations/attributes are powerful features which make all kinds of interesting things possible at runtime, like ORM mapping tools and a host of dynamic code generation schemes–but I get the impression that metadata tags are not very useful at runtime in AS3, that they are more compile-time features. In any case, yes, PLEASE write the tutorial!

  6. Youd Fred Garcia Says:

    Jacob, I am new to Flex and my background if from .NET. I found interesting the information regarding custom metadata tags. Could you please send me a tutorial if possible.

    I would like to know if there is the posibility to associate that metadata tag to a concrete object by the compiler, or if I will have to used “describeType” to dehydrate the concretes at runtime. what are the alternatives

    Thanks,
    Youd

  7. Easy object reflection with Spicelib at Marc’s Musings Says:

    […] this blog entry for a little info on how to do custom […]

  8. toddy Says:

    Could you please explain detail about custom metadata tage usage?

    I look for the approach urgent

  9. Jacob Wright Says:

    I don’t have time right now for a full tutorial on using custom metadata. I’ll work on one at some point. But if you use “-keep-as3-metadata Listen” in your Flex Library Project’s properties under “Additional Compiler Options” then when you have a class with the metadata [Listen(arg=”value”)] above it, or above a method or property, you can see that metadata in the XML of the flash.utils.describeType method.

    You can get more information here:
    http://www.dreaminginflash.com/2008/01/26/metaprogramming-actionscript3-metadata-in-flex-builder-3/
    and here:
    http://coenraets.org/blog/2007/10/annotating-actionscript-classes-with-custom-metadata-simple-orm-framework-for-air/

  10. Praveen Says:

    Nice blog. It would be helpful if you tell more detail about “-keep-as3-metadata MyTag” and the process to add it in lib. Awaiting your response.

  11. Lex Says:

    Wow! Almost like .Net. :)
    This feature could be very useful, thanks.

  12. Justin Says:

    Awesome

  13. ActionScript Metadata Tags « GDTech Says:

    […] 1. Since Flex3, any application that uses a SWC compiled with “-keep-as3-metadata” will not have the metadata specified by the SWC stripped out by the Flex compiler [Reference] […]

  14. Enrique Says:

    3 easy steps for custom Metadata:
    http://cookbooks.adobe.com/post_Custom_metadata_in_three_simple_steps_-11907.html