Optimize a Flex application using deferred instantiations

來源:互聯網
上載者:User

[轉載]http://cookbooks.adobe.com/post_Optimize_a_Flex_application_using_deferred_instant-15826.html

Problem

I have a flex application containing custom components.
How can I add them to my Flex Application and make sure it's optimized ?

Solution

We'll use the "deferred instantiation" which
mean creating and adding our component's children when we need to (typically
when they have to be displayed).

Detailed explanationMy custom
component

I have a custom component extending the Container Class.
It's really simple as it only displays a picture :

WizardCat

package com.palleas.component
{
  import flash.display.DisplayObject;
  
  import mx.core.Container;
  import mx.core.UIComponent;
  
  public class WizardCat extends
Container

  {
    [Embed(source="http://www.cnblogs.com/../assets/wizardcat.png")]
    protected var
CatClass:Class;

    
    public function WizardCat()
    {
      super();
     
trace("creating wizard cat !");

      var
cat:DisplayObject = new CatClass() as DisplayObject;

      var
catContainer:UIComponent = new UIComponent();

     
catContainer.addChild(cat);

     
addChild(catContainer);

    }
  }
}

Doing this is really wrong and can be a serious
performance issue. Furthermore, components extending the Container class (such
as ViewStack, for example) sometimes defers the creation of their children due
to the "creationPolicy" property not set to "ALL". This
behavior is not here to annoy you but to optimize Flex application loading :
instead of automatically draw a component, it will wait until the component is
really needed, when it's displayed.

Deferred
instantiation

To do the same thing properly, override the
"createChildren()" method and create your children there. This way,
even if the component is instanciated, it'll wait the application explicitly
calls its createChildren() method to create its children (here, it's just a
picture) :

package com.palleas.component
{
  import flash.display.DisplayObject;
  
  import mx.core.Container;
  import mx.core.UIComponent;
  
  public class WizardCat extends
Container

  {
   
[Embed(source="http://www.cnblogs.com/../assets/wizardcat.png")]

    protected var
CatClass:Class;

    
    public function WizardCat()
    {
      super();
    }
    
    override protected
function createChildren():void

    {
     
super.createChildren();

     
trace("creating wizard cat !");

      var
cat:DisplayObject = new CatClass() as DisplayObject;

      var
catContainer:UIComponent = new UIComponent();

     
catContainer.addChild(cat);

     
addChild(catContainer);

    }
  }
}

As you can see in the picture below, it's still working !

Conclusion

The conclusion of this recipe if quite simple : avoid
instanciating and adding children components in constructor. This way, your
application should run a little bit smoother!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.