back to krasimirtsonev.com
to blog's home page

AS3: Using custom metadata in flex (part 2)

Because of the huge interest in AS3: Using Custom Metadata in Flex (part 1) I decided to dig a little bit deeper in this topic. You already know how to get information about your custom metadata, so in this article I'll show you how to use it.

The biggest problem in the usage of custom metadata is that you should add all your custom tags in the additional compiler arguments. If you have a lot of tags you should make a lot of changes in this direction. That's why I created a SWC component that will make your life easier. Just download the swc file from here and include it in your flex project. The idea is that the swc component is compiled with -keep-as3-metadata * argument, which means that if you include it in your project you can use tag with a name *. The swc file contains two classes:
  • com.krasimirtsonev.data.metadata.MetadataParser
    The purpose of this class is to fetch the xml that contains information about the metadata and composes objects by type MetadataVO
  • com.krasimirtsonev.data.metadata.MetadataVO
    Value object that contains structured data about every metadata tag


So, let's see a simple example of using these classes. We have a basic application class:
It is really simple. We created an object by type MyCustomClass and printed a property name of this object. Here is the interesting part:
As you can see the property name of MyCustomClass is empty and basically we didn't put any data into it. But if you run the application will see:
The CUSTOM_METADATA_EXAMPLE string comes from an additional class called Storage:
The Storage class has just one static method which returns that string if we pass a correct key (in our case this is name-of-the-app). The magic part is hidden in this line of code:
The MetadataParser has only one method init which accepts two parametrs. The first one is your class (the class that contains the metadata) and the second one is class that will operate with your custom tags. Here is the content of my analyser:
What happens is that the MetadataParser parses the metadata of MyCustomClass and converts it to an array of MetadataVO, which is passed to the constructor of our analyser. We can set as many attributes as we want and they will be available. In this current example I checked if the doThisOperation attribute has value of get-from-storage and if yes then I called the getData method of the Storage class with a key equal to the value of parameterToTheOperation attribute.
Here is the content of the MetadataVO class:

  • xml - contains the xml representing the tag
  • classObj - reference to your class object. The one that calls MetadataParser.init. In most cases you will use it to access some public properties or methods.
  • metadata - an object that contains all the attributes of your metadata tag
  • metadataFor - string that shows where the tag is placed (i.e. before variable, method or class definition)
  • name - string that shows the name of the item after the tag (i.e. if you place the tag before variable with name myVar, this property will return myVar)
  • type - string that shows the type of the variable (if the tag is placed before variable)
  • params - if you place the tag before the method then this property returns information regarding the parameters of that method
  • declaredBy
  • returnType - string that shows the returned type of the method (if the tag is placed before the method)


And here is the code of the parser:


As a conclustion I can say that by using the swc component you can parse your custom metadata in flex without adding anything to your compiler's path.

The source code of the example is available here.

Sharing ...
Commenting ...
blog comments powered by Disqus