Metaprogramming with Actionscript3 Metadata in Flex Builder 3
I first became interested in metaprogramming and metadata in Actionscript3 after reading about it on Ely Greenfield's blog here. What caught my eye at first glance was the use of flash.utils.describeType():XML, a method I've never seen anyone use in the past year of programming and reading about AS3.
If you call describeType(this) on a class, the method returns information only about nonstatic public members of the class. Its purpose is to do 'object introspection', or in other words, determine the elements of a class at run time, such as its properties and methods. This is equivalent to use a for..in loop and sweep the properties of an object.
If you call it in conjunction with getDefinitionByName(), e.g. describeType(getDefinitionByName(”MyClass”)), the method returns information only about the target’s static members.
Now what Ely says in his blog is:
Towards the end of the Flex 2 development cycle, we slipped a tiny feature into the flash player API that allowed us to get access to some actionscript metadata at runtime via the describeType() API. In Flex 2.01, we made it possible for the end devleoper to define what metadata is preserved at runtime. In Flex 3, we’ve made it even easier to preserve metadata. A couple of people have discovered this feature, and started to do some really interesting things with it.
Ely continues to show an example of the possible usage of metadata and presents a demo that he calls 'Declarative Binding and Handlers through Custom Metadata' on one page and 'metaprogramming with custom actionscript metadata' on another. But it's really interesting, you should check it out (if you've been at MAX Barcelona you may have already).
So I wanted to try playing with metadata and my first experiments weren't all that successful. I was able to compile Ely's demo but failed at first to do my own. I then googled a bit and found another post on 'Creating Own Meta Data in Flex'. I tried to compile the code but compiling the application with -load-config+="meta-config.xml" would yield an error. I checked the command line options of mxmlc and it seems the way in mxmlc 3.0 build 189825 is to use a space between -load-config and the xml file. Oh well, nevertheless, metadata is here and it's fun. Here's the source and options that worked for me.
index.mxml
MetaDataExample.as
meta-config.xml
Compiler parameters:-locale en_US -keep-as3-metadata Orange -load-config "/Users/nuno/Desktop/Projects/FlexWorkspace/MetaDataTest/src/meta-config.xml"
This was my first experiment with metadata in Actionscript3. I think I'll be playing more with this because it's fun to create metadata and act accordingly at run-time when loading an SWF file or various other situations.
January 26th, 2008 at 4:47 am
and maybe you will come up with nice ideas on how to use this thing =)
March 4th, 2008 at 8:54 pm
I like to build a set of functions in AS3 which let me dynamically interact via XML from a Flex application to a Ruby on Rails application.
This sounds like the perfect example or application to use it… would like to have:
URL: http://localhost/
Controller: projects
Generate CRUD methods to allow via XML make the properly access to the URL.
READ Projects: [get] http://localhost/projects.xml
NEW Project: [post] http://localhost/projects.xml
UPDATE Project (n): [post] http://localhost/projects/n.xml&_method=DELETE
DELETE Project (n): [ post] http://localhost/projects/n.xml&_method=DELETE
Any advise on howto create this script will be really appreciate it.
Keep doing the great job.
Dinooz