blog.ashier.com flash / flex / air development
rss rss comment

Deferred instantiation is a common technique used to improve Flex application start-up time and to control when and how to instantiate child components within a container when the creationPolicy is set to none.

Last week, I was able to implement this into an application we're currently developing at work. It took me a while to figure it out but I must say, it is really handy. A ViewStack for example will allow switching from one child to another, but as the child of that container gets to be really complicated, it may highly affect your application's performance.

Why???
If no creationPolicy is specified for a container, that container inherits its parent's creationPolicy. If no creationPolicy is specified for the Application, it defaults to ContainerCreationPolicy.AUTO.

This is where Deferred Instantiation can save your day.

I have below a basic example of how you can implement Deferred Instantiation using createComponentFromDescriptor()

createComponentFromDescriptor allows you to create one child at a time.

Actionscript:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application
  3.     xmlns:mx="http://www.adobe.com/2006/mxml"
  4.     creationComplete="onCreationComplete()"
  5.     layout="absolute"
  6.     xmlns:components="components.*">
  7.    
  8.     <mx:Script>
  9.         <![CDATA[
  10.        
  11.             import mx.core.Container;
  12.             import mx.core.UIComponentDescriptor;
  13.             import mx.controls.Button;
  14.            
  15.             private var vsChildren:Array = new Array();
  16.            
  17.             private function onCreationComplete():void {
  18.                 for(var i:int = 0; i <vs.childDescriptors.length; i++) {
  19.                    
  20.                     var lbl:String = "";
  21.                     var descriptor:UIComponentDescriptor = UIComponentDescriptor(vs.childDescriptors[i]);
  22.                     var item:Button = new Button();
  23.                    
  24.                     item.addEventListener(MouseEvent.CLICK, onClick);
  25.                     item.label = descriptor.properties.label;
  26.                     item.data = {index:i};
  27.                     cb.addChild(item);
  28.                 }
  29.             }
  30.            
  31.             private function onClick(e:MouseEvent):void {
  32.                 var index:int = e.target.data.index;
  33.                 if(!vsChildren[index]) {
  34.                     vsChildren[index] = vs.createComponentFromDescriptor(vs.childDescriptors[index], true) as Container
  35.                     vs.selectedChild = vsChildren[index];
  36.                     vs.validateNow();
  37.                 }else {
  38.                     vs.selectedChild = vsChildren[index];
  39.                 }
  40.             }
  41.            
  42.         ]]>
  43.     </mx:Script>
  44.     <mx:VBox width="100%" height="100%" paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10">
  45.         <mx:HBox id="cb" width="100%"  horizontalAlign="center" verticalAlign="middle"/>
  46.         <mx:ViewStack id="vs" width="100%" creationPolicy="none" height="100%">
  47.             <components:Red label="Red" width="100%" height="100%" />
  48.             <components:Green label="Green" width="100%" height="100%" />
  49.             <components:Blue label="Blue" width="100%" height="100%" />
  50.         </mx:ViewStack>
  51.     </mx:VBox>
  52. </mx:Application>

SWF:
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Download the source code here

Tags: ,

One Response to “Deferred Instantiation”

  1. Construção Adiada (Deferred instantiation) « iuricmp Says:

    [...] http://blog.ashier.com/2008/04/07/deferred-instantiation/ Deixe um comentário [...]

Leave a Reply