Friday, August 26, 2011

About Flex4.5 Global Variable Bindable

In Flex4.5, a Bindable variable defined in main Application MXML cannot be directly used to binding data in the custom components which are created in different packages. The following code shows how to let all the other components access a global variable.

In the Application MXML, define a bindable variable
  1. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  2.   xmlns:s="library://ns.adobe.com/flex/spark"
  3.   xmlns:mx="library://ns.adobe.com/flex/mx">

  4. <fx:Script>
  5. <![CDATA[

  6. [Bindable]
  7. public var globalVariable:String = "Global Variable";

  8. ]]>
  9. </fx:Script>

  10. </s:Application>
In the component, for example, a Panel

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009
  3. xmlns:s="library://ns.adobe.com/flex/spark
  4. xmlns:mx="library://ns.adobe.com/flex/mx">

  5. <fx:Script>
  6. <![CDATA[

  7. import mx.core.FlexGlobals;

  8. [Bindable]
  9. private var application:Object = FlexGlobals.topLevelApplication;

  10. ]]>
  11. </fx:Script>

  12. <s:TextInput text="{application.globalVariable}"/>

  13. </s:Panel>
In this case, the text value of the textInput controls in the Panel will bind to the globalVariable defined in Application MXML

No comments:

Post a Comment