As you probably know you can use your ActionScript 3.0 classes declaratively as MXML tags in Flex projects. When you do so you can declare properties inline in the tag:

or the same can be done using a child tag with the property name you want to target:


  This is the text value declared inside a child tag

 
There was one thing that puzzled me until recently and thanks to Cyril Hanquez I've now figured out how to do this and made the connection to something Marco Casario blogged about earlier.

What if you have an Array property inside of your class and want to declaratively add instances of only a particular class to it? Enter the ArrayElementType meta data tag.

Imagine the following scenario:


  
    
    
    
    
  

The code above says that you have a Meeting class with an attendees Array property and to that property you add instances of a Person class.

Lets look at the two ActionScript 3.0 classes:

 
Meeting.as

package com.peterelst.example {

  public class Meeting {
    [ArrayElementType("com.peterelst.example.Person")]
    [Bindable] public var attendees:Array = new Array();
  }

}

Person.as

package com.peterelst.example {

  public class Person {
    [Bindable] public var name:String;
  }

}

 

Using the ArrayElementType meta data tag you can simply specify the fully qualified class name you want this Array property to accept.

Now lets bring these two classes together in a little example where data binding is used to populate a List component with our attendees Array property.




  
    
      
      
      
      
    
  

  	


 

If you now get an item from the List component you'll have an instance of the Person class to work with.

Important to note is that the ArrayElementType meta data tag is only checking the type at compile time and won't throw an error if a different data type gets added to the array at runtime.

If you target Flash Player 10 with your Flex application you could also look into using the new Vector data type (which is essentially a dense mono-typed array) to accomplish the same thing.

 

Posted
AuthorPeter
CategoriesFlex