How to create and to work with custom metadata in ActionScript3/Flex?
Asked
Active
Viewed 3,104 times
11
-
Really good questions & answers. Could someone please list some examples of how this would be useful though? Thanks! – chamberlainpi Apr 20 '11 at 12:34
-
@bigp Some architectural frameworks use custom metadata (Parsley, Swiz, for example) – Timofei Davydik Apr 20 '11 at 12:38
-
Will these custom metadata work in Flash CS* IDEs as well when compiling? – chamberlainpi Apr 20 '11 at 20:03
-
@bigp it should. You deal with classes and objects like in other IDEs, so reflection should work – Timofei Davydik Apr 20 '11 at 20:06
2 Answers
19
There is two general directions to deal with metadata in ActionScript in Flex:
- Dealing in runtime using reflection (or introspection in terms of Adobe). You can find documentation here. There are several third-party libraries which provides more convenient reflection API such as as3-commons-reflect or reflection API in Parsley.
- Using Flex Compiler Extensions which was introduced since Flex 4 SDK revision 8165 (see details). See also the corresponding discussion and official documentation.

Constantiner
- 14,231
- 4
- 27
- 34
15
To create metadata, just use square bracket syntax in your class definition:
[ClassAttr]
public class ClassWithMetadata {
[AttributeNoArgs]
public var data:Object;
[AttributeWithArgs(arg="value)]
public var prop:Object;
}
Call describeType(ClassWithMetadata)
to get xml description of your class. Metadata will be there, you can parse it and process as you like.
To prevent compiler from stripping your metadata, add compiler argument -keep-as3-metadata Attribute
for each attribute name you using. This can be done in library settings or in each project that uses metadata.

alxx
- 9,897
- 4
- 26
- 41
-
-
Is this unique to the Flex compiler (mxmlc), or will the metadata be retained in the Flash Professional compiler as well? – Triynko Dec 09 '13 at 20:32