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

About Flex4.5 creationPolicy

Containers define a creation policy that specifies when its children are created. By default, a container delays creating its children until they are needed by the application. This process is called deferred instantiation. Since all children are not created at application start up, your application appears to run faster.

You can control the creation policy for a container. For example, you can create all children at startup, or you can explicitly determine when a child is created.

You can use the following creation policies:
  • ContainerCreationPolicy.AUTO 
    • The container delays creating children until they are needed.
    • For navigator containers such as the MX ViewStack, MX TabNavigator, and MX Accordion containers, the container creates its direct children immediately, but waits to create the descendants of each child until the child needs to be displayed. As a result, only the initially required child or children of a container get processed past the preinitialization stage. An auto creation policy produces the best startup time because fewer components are created initially.
  • ContainerCreationPolicy.ALL
    • All container children are created and initialized before the container is initialized.
  • ContainerCreationPolicy.NONE
    • Requires the container to explicitly create every child instance.

For detailed information on creation policies, see Improving startup performance.