views:

1088

answers:

2

Hi, Given the following:

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2009/mxml"&gt;
    <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.

+2  A: 

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>
dirkgently
A: 

The compiler actually treats child components of containers as a special case. Take a look at the childDescriptors property of mx.core.Container for some explanation. When you create a Flex component instance in MXML, it isn't instantiated immediately. Instead, a "descriptor" is created, and that is used to instantiate the component at some future time, as determined by the container's creationPolicy property. If you add the -keep-generated-actionscript argument (or the shortened version, -keep) to your compiler arguments, you'll be able to see the AS3 code that the compiler generates from MXML.

joshtynjala