Adobe Flex 2 Create and Extend Components

Page 100

When a property is the source of a data binding expression, any changes to the property must signal an update to the destination property. The way to signal that change is to dispatch an event, as the following example shows: <?xml version="1.0"?> <!-- mxmlAdvanced/myComponents/StateComboBoxSetGetBinding.mxml --> <mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <![CDATA[ import flash.events.Event; private var stateArrayShort:Array = ["AK", "AL"]; private var stateArrayLong:Array = ["Arkansas", "Alaska"]; private var __shortNames:Boolean = true; public function set shortNames(val:Boolean):void { __shortNames = val; if (__shortNames) { dataProvider=stateArrayShort; } else { dataProvider=stateArrayLong; } // Create and dispatch event. dispatchEvent(new Event("changeShortNames")); } // Include the [Bindable] metadata tag. [Bindable(event="changeShortNames")] public function get shortNames():Boolean { return __shortNames; } ]]> </mx:Script> </mx:ComboBox>

To use a property as the source of a data binding expression: 1.

Define the property as a variable, or by using setter and getter methods. You must define a setter method and a getter method if you use the [Bindable] tag with the property.

2.

100

Insert the [Bindable] metadata tag before the property definition, or before either the setter or getter method, and optionally specify the name of the event dispatched by the property when it changes.

Creating Advanced MXML Components


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.