R – Flex compiler metadata “DefaultProperty”

actionscript-3apache-flexmxml

Given the following:

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2009/mxml">
    <mx:Panel title="blah">
       <mx:Button />
    </mx:Panel>
</mx:Application>

Can you tell me where child elements (ex. mx:Button) are assigned in parent elements (ex. mx:Panel) by default by mxmlc. You can set the "DefaultProperty" compiler metadata tag to specify where they are assigned but what does flex do when it is not specified.

For example I've traversed the source of all the flex classes mx:Panel inherits from and DefaultProperty is never mentioned, leading me to wonder what the default value of DefaultProperty.

sorry for any noobishness but i've read the docs inside out.

Best Answer

When writing AS based components, the default property lets you specify a property that you can use as a child-tag. Eg:

 <MyComp:TextAreaDefaultProp>Hello</MyComp:TextAreaDefaultProp>

You could as well have used:

 <MyComp:TextAreaDefaultProp defaultText="Hello" />

What happens when you don't specify? You don't get a value for that property. Given the following component:

package
{
    // as/myComponents/TextAreaDefaultProp.as
    import mx.controls.TextArea;

    // Define the default property.
    [DefaultProperty("defaultText")]

    public class TextAreaDefaultProp extends TextArea {

        public function TextAreaDefaultProp() 
        {
            super();
        }       

        // Define a setter method to set the text property
        // to the value of the default property.
        public function set defaultText(value:String):void {
            if (value!=null)
            text=value;
        }

        public function get defaultText():String {
            return text;
        }
    }    

}

Run this snippet:

 <?xml version="1.0" encoding="utf-8"?>
 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
            layout="absolute" width="535" height="345"
            xmlns:local="*">
 <mx:VBox>
 <local:TextAreaDefaultProp id="a" defaultText="Hello"/>
 <local:TextAreaDefaultProp id="b" > World </local:TextAreaDefaultProp>
 <local:TextAreaDefaultProp id="c" />
 <mx:TextArea id="z"/>
 <mx:Button  click="{z.text = a.defaultText 
                                + ' ' + b.defaultText
                                + ' ' + (c.defaultText.length);}" />

</mx:VBox>
</mx:Application>
Related Topic