Optimize a flex application using deferred instantiations

Source: Internet
Author: User

Http://cookbooks.adobe.com/post_Optimize_a_Flex_application_using_deferred_instant-15826.html

Problem

I have a flex application containing componm 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
"Createchildren ()" method and create your children there. This way,
Even if the component is instated, It'll wait the application explicitly
Callits createchildren () method to create its children (here, it's just
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
Instancating and adding children components in constructor. This way, Your
Application shoshould run a little bit smoother!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.