[转载]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 explanation

My 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!

转载于:https://www.cnblogs.com/Bill_Lee/archive/2011/05/17/2048292.html

Optimize a Flex application using deferred instantiations相关推荐

  1. Flex Application里的addChild()

    转自:http://www.cnblogs.com/jiahuafu/archive/2009/03/20/1417679.html 在Flex Application里,是不能直接用addChild ...

  2. 【转】Flex Application 初始化顺序

    转自:http://www.jexchen.com 大家都知道,我们在编写Flex应用程序时,通常是以<mx:Application>标签作为开头,实际上,Flex应用程序在启动运行的时候 ...

  3. Adobe Flex Application LifeCycle

    Flex的根对象就是SystemManager,而不是我们平常看到的Application. SystemManager是第一个在Flex应用中被创建的Display Class,继承自flash.d ...

  4. Flex Application初始化顺序

    FLEX生成的SWF运行的顺序是 preloader->systemManager->FlexApplication started... 然后才是 preinitialize 在所有的初 ...

  5. Flex入门的好书——《Flex3 Cookbook 中文版》

    本文标题:Flex入门的好书--<Flex3 Cookbook 中文版> 本文链接:http://zengrong.net/post/659.htm   Flex3 Cookbook 中文 ...

  6. 推荐60+ Flex开发参考网站

    推荐60+ Flex开发参考网站 下面是一些好的Flex开发的网站或者Flex资源,如果你使用Flex开发,可以参考一下. 网上找的,可以参考参考!呵呵 新手入门参考: Adobe Flex 3 - ...

  7. 动态加载flex皮肤.

    动态加载swf文件作为flex 的皮肤 There are lots of great resources out there on how to skin your Flex application ...

  8. Flex入门搭建j2EE开发环境

    本文介绍 Flex 开发的基础知识:包括如何搭建开发环境,如何建立和部署简单的 Flex 项目.首先你的机器确保安装了jdk+eclipse(或者MyEclipse)+tomcat,现在你所需要的只是 ...

  9. Flex 中取得当前服务IP地址

    var uri:URI = new URI(Application.application.url); trace(uri.authority);//IP address of the server ...

最新文章

  1. 手把手教你在Python中实现文本分类(附代码、数据集)
  2. ACM之常见的(C++版)问题解析
  3. doker zookeeper kafka单机搭建
  4. 嵌入式深度学习运用的思路
  5. 相机标定(二)深入理解四大坐标系与其变换关系
  6. mysql创建多实例,mysql 单服务器创建多实例
  7. java中的fd是什么意思_java中关键字和保留字分别是什么意思
  8. 最新 HTTP/2 漏洞曝光,直指 Kubernetes!
  9. 发那科程序全部输出_走,去看看发那科机器人全新的自动化解决方案!
  10. CSS样式表——列表与布局
  11. linux下批量查找UTF-8的BOM文件,并去除BOM
  12. NSIS 注册DLL OCX
  13. 28-地理空间数据云下载
  14. 1000并发的系统服务器配置,1000并发服务器配置
  15. 日语学习网站分类汇总
  16. FZU 1573 大学自习室
  17. web技术分享| 前端秘籍之“易容”术
  18. 【安全知识分享】2021年安全生产月主题宣讲课件(附下载)
  19. jquery canvas网页画布画图
  20. 重磅|云迹科技获金茂资本、携程集团、光控众盈,海银资本联合投资...

热门文章

  1. Ubuntu 下安装adobe flash player
  2. 排序1+1:冒泡排序法(BubbleSort)的改进以及效率比较
  3. linux lnmp1.5 部署laravel项目
  4. 打印modal框中在线生成的二维码
  5. Android 的 dex2jar 和 jd-gui 反编译 apk 源代码
  6. 一款炫酷Loading动画--载入成功
  7. 摩尔定律会死亡吗 芯片到底可以变得有多小
  8. IE7及以下浏览器不支持json的解决方法
  9. mysql事务,START TRANSACTION, COMMIT和ROLLBACK,SET AUTOCOMMIT语法
  10. 使用 Cuttlefish 虚拟 Android 设备