Adobe Flex 3 Help

Using an E4X expression in a data binding expression

A binding expression in curly braces or an <mx:Binding> tag can contain an ECMAScript for XML (E4X) expression when the source of a binding is a bindable property of type XML. You cannot use E4X when defining a data binding by using the BindingUtils.bindProperty() or the BindingUtils.bindSetter() method.

Using an E4X expression in curly braces

A binding expression in curly braces automatically calls the toString() method when the binding destination is a String property. A binding expression in curly braces or an <mx:Binding> tag can contain an ECMAScript for XML (E4X) expression when the source of a binding is a bindable property of type XML; for more information, see Using an E4X expression in an <mx:Binding> tag.

In the code in the following example, there are three binding expressions in curly braces that bind data from an XML object. The first uses . (dot) notation, the second uses .. (dot dot) notation, and the third uses || (or) notation.

<?xml version="1.0"?>
<!-- binding/E4XInBraces.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[
        
            [Bindable]
            public var xdata:XML = <order>
                <item id = "3456">
                    <description>Big Screen Television</description>
                    <price>1299.99</price><quantity>1</quantity>
                </item>
                <item id = "56789">
                    <description>DVD Player</description>
                    <price>399.99</price>
                    <quantity>1</quantity>
                </item>
            </order>;
        ]]>
    </mx:Script>

    <mx:Label text="Using .. notation."/>
    <!-- Inline databinding will automatically call the 
        toString() method when the binding destination is a string. -->
    <mx:List width="25%" 
        dataProvider="{xdata..description}"/>
    
    <mx:Label text="Using . notation."/>
    <mx:List width="25%" 
        dataProvider="{xdata.item.description}"/>

    <mx:Label text="Using || (or) notation."/>
    <mx:List width="25%" 
        dataProvider="{xdata.item.(@id=='3456'||@id=='56789').description}"/>
</mx:Application>

The executing SWF file for the previous example is shown below:

Using an E4X expression in an <mx:Binding> tag

Unlike an E4X expression in curly braces, when you use an E4X expression in an <mx:Binding> tag, you must explicitly call the toString() method when the binding destination is a String property.

In the code in the following example, there are three binding expressions in curly braces that bind data from an XML object. The first uses . (dot) notation, the second uses .. (dot dot) notation, and the third uses || (or) notation.

<?xml version="1.0"?>
<!-- binding/E4XInBindingTag.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    width="600" height="900">

    <mx:Script>
        <![CDATA[
            [Bindable]
            public var xdata:XML = 
                <order>
                    <item id = "3456">
                        <description>Big Screen Television</description>
                        <price>1299.99</price><quantity>1</quantity>
                    </item>
                    <item id = "56789">
                        <description>DVD Player</description>
                        <price>399.99</price>
                        <quantity>1</quantity>
                    </item>
                </order>;        
        ]]>
    </mx:Script>

    <mx:Label text="Using .. notation."/>

    <!-- This will update because what is 
        binded is actually the String and XMLList. -->
    <mx:List width="75%" id="txts"/>
    <mx:Binding 
        source="xdata..description" 
        destination="txts.dataProvider"/>

    <mx:Label text="Using . notation."/>
    <mx:List width="75%" id="txt2s"/>
    <mx:Binding 
        source="xdata.item.description" 
        destination="txt2s.dataProvider"/>

    <mx:Label text="Using || (or) notation."/>
    <mx:List width="75%" id="txt3s"/>
    <mx:Binding 
        source="xdata.item.(@id=='3456'||@id=='56789').description" 
        destination="txt3s.dataProvider"/>
</mx:Application>

The executing SWF file for the previous example is shown below: