When trying to create a form with a combo box and a text input I
happened upon what would appear to me to be a quirk with how explicit
casts affect {} binding.  Essentially what I was trying to do was have
a single field on a model class be exposed by both a combo box and a
text input.  The idea was that the user could pick one of the
predefined options in the combo box, or an 'other' entry and then
supply their own freeform value in the text box.  The twist for this
form is that it extends a 'super-class' form that accepts a
'super-class' model object.  What this means for the form is that it
needs to explicitly cast the model object for all the bindings.

The end result was I found that this syntax did not work...
<mx:TextInput id="textIn" text="{(item as MyItem).name}" />

But this syntax does work...
<mx:TextInput id="textIn" text="{MyItem(item).name}" />

Has anyone else come across this, or know why this might occur? 

Full app example below...
example.mxml 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
                layout="absolute" preinitialize="preinit()">

        <mx:Script>
                <![CDATA[
                        import AbstractItem;
                        import mx.collections.ArrayCollection;
                        import MyItem;
                        
                        [Bindable]
                        //Some dummy data for example use.
                        private var comboData:ArrayCollection = 
                                new ArrayCollection(["1", "2", "3"]);
                        
                        [Bindable]
                        private var item:AbstractItem;
                        
                        private function preinit():void {
                                item = new MyItem();
                                (item as MyItem).name = "2";
                        }
                ]]>
        </mx:Script>
        
        <mx:Panel width="500" height="300" title="Explicit Cast Binding 
Example">
                
                <mx:HBox paddingTop="5" paddingLeft="5">
                        
                        <!-- 
                                Any change to the combo box should update the 
'data' field on 
                                        'item' and then be reflected in the 
text box via the binding. 
                        -->
                        <mx:ComboBox id="combo" 
                                        dataProvider="{comboData}"
                                        change="(item as MyItem).name = 
combo.selectedItem as String" />
                                        
                        <!-- 
                                Text field should always display the 'data' 
field of the 
                                        'item' instance.  
                                        
                                Use this to get it to work.
                                <mx:TextInput id="textIn" 
text="{MyItem(item).name}" />
                        -->
                        <mx:TextInput id="textIn" text="{(item as 
MyItem).name}" />
                        
                </mx:HBox>
        
        </mx:Panel>
        
</mx:Application>

AbstractItem.as
package
{
        import flash.events.EventDispatcher;
        
        public class AbstractItem extends EventDispatcher
        {                       
                [Bindable]
                public var data:String;
        }
}

MyItem.as
package
{
        public class MyItem extends AbstractItem
        {
                [Bindable]
                public var name:String; 
        }
}

Reply via email to