天天看点

关于flex的module开发

前台的开发,目前RIA技术越来越具有优势,而其中的flex目前也出到了版本3. 之前采用flex开发展现层的时候,经常为程序越做越大而发愁,今天了解到了flex的module这块的功能,打算通过一两天时间学习和了解。之后进行我们之前项目的前台重构工作。

flex的Modules技术是可以被flex程序使用的一个swf文件,它不能脱离程序独立运行,但是多个程序之间可以共享它。flex的Modules技术将应用程序分割成小块、模块,主程序动态的加载所需要的模块。主程序在启动时并不全部加载所有的模块。当用户和模块没有交互的时候它不需要加载模块,同时它在模块不需要的时候可以卸载模块并释放内存和资源。

flex的Modules技术主要有如下的优点: 让swf文件初始下载尺寸更小 让加载时间更短 对应用程序更好的封装性。

一个Module是一个特殊的可以动态加载的包含IFlexModuleFactory类工厂的swf文件。它允许应用程序在运行时加载代码并在不需要主程序相关连类实现的情况下创建类的实例。

Module和RSLs(运行时共享库)很相似,他们独立分离代码将应用程序分成独立的加载的swf文件。但它比RSLs更加灵活,因为它可以在运行时动态加载/卸载并且不依赖于应用程序编译。

使用Modules的两个典型的应用场景是不同用户路径的大型应用程序和门户应用程序。 (分析略)

Modules实现了标准的类工厂接口。

通过使用共享的接口定义,减少了模块和shell之间的硬依赖(耦合)。提供了一种类型安全的沟通机制并在没有增加swf文件大小的情况下增加了一个抽象层。

下面图片展示了modules和shell之间接口的关联关系

关于flex的module开发

ModuleManager 负责管理加载了的modules集合,将之对待为按照module URL为索引的单例map。加载module会触发一系列的事件来让客户端监控module的状态。module只加载一次,但是会重载并分发事件,因此客户端代码可以简单的依赖 READY 事件来获取module的类工厂来使用它。

ModuleLoader类在ModuleManager API之上最高级的API,它提供了一个基于module架构的简单实现,但是ModuleManager提供了module之上的更好的控制。

默认下,一个module会被加载进当前应用程序domain的子domain。你可以通过使用ModuleLoader的applicationDomain的属性来指定不同的应用程序domain。

因为module被加载到子domain中,因此它拥有的类将不属于主应用程序的domain中。举例:当一个module加载了PopUpManager类,那么在整个应用程序中它就变成了PopUpManager类的Owner,因为它注册到SingletonManager中。加入另外一个module晚些试图使用PopUpManager,Adobe ® Flash® Player 将会抛错。

解决方法是确保如PopUpManager、DragManager这些managers和一些共享服务都定义在主应用程序中(或者晚加载入应用程序domain中)。这样这些类可以被所有的module使用。典型的,都是通过将他们添加到脚本块中:

import mx.managers.PopUpManager;
import mx.managers.DragManager;
private var popUpManager:PopUpManager;
private var dragManager:DragManager;      
这项技术同时也应用到components,当module第一次使用component时,将在它自己的domain中拥有这些component的类定义。作为结果,如果别的module试图使用这些已经被另一个module使用的component,它的定义将会不能匹配到现存的定义中。因此,为了避免component的定义不匹配,在主应用程序中创建component的实例,让所有的module去引用。 



因为module必须和应用程序再同一个security domain中,当你在AIR应用程序中使用任何的module 
SWF文件时,必须和主应用在相同的目录或者在其子目录中,确保他们在相同的security沙盒中。一个确保的方法是使用URL引用module位置不要包含“../”。

创建模块化的应用程序: 


1 创建任意数量的modules。基于MXML文件的根节点是<mx:Module>。基于AS的扩展Module或者ModuleBase类。 


2 
编译module,方式如同应用程序的编译。可以使用基于命令行的mxmlc或者集成于flexbuilder中的编译器。 


3 创建一个应用程序类。 


4 在应用程序文件中使用<mx:ModuleLoader>来加载modules。或者你也可以使用mx.modules.ModuleLoader和mx.modules.ModuleManager类的方法来加载。 


编译module
使用命令行举例:mxmlc 
MyModule.mxml
编译后生成一个swf文件,它不能独立运行也不能被浏览器执行,它只能被应用程序加载为module。它不能被Adobe® 
Flash® Player、Adobe® AIR,™ 、浏览器所直接运行。 


当编译module后,你应当试图删除掉module和应用程序之间的那些冗余文件。你可以通过创建应用程序的link 
report来达到这个目的。当然了,flexbuilder可以自动的为你做这些。 

减小module的大小 


module的尺寸主要由module所使用的component和类来决定。默认情况下,module包含它所依赖的所有component的框架代码,这可能会导致其尺寸非常的大。 



要想减小其尺寸,你可以通过指令让它导出应用程序class来优化它。只让module包含它所需要的类。 


用命令行编译器来做的话,将在包含module的应用程序中产生一个linker 
report。在编译时采用load-externs选项。这个过程在flexbuilder中也需要。 


用命令行编译器创建和使用linker report 


1 产生linker report 和编译应用程序 


mxmlc -link-report=report.xml MyApplication.mxml 


默认的linker report输出位置和编译器输出问题相同,一般为bin目录。
       
2 编译module并传递linker report到load-externs选项      
mxmlc -load-externs=report.xml MyModule.mxml      
重新编译module

如果module和应用程序在相同的工程下,那么当module改变时并不需要重新编译应用程序。这是因为应用程序是运行时加载module,而在编译期并不强制检查。

同样当你改变应用程序时也不需要重新编译module。

如果module和应用程序在不同的工程下,必须重新编译module。

但是如果当改变有可能影响linker report或代码,你必须重新编译相应的应用程序和module。

               
 

调试module

 

调试使用了module的应用程序,你必须设置调试编译器选项为true。否则你将不能在module中设置中断点或在他们中间收集调试信息。

在flexbuilder中调试选项默认是开启的。在命令行,调试是默认关闭的。

 

一个很普遍的问题是如果一个module包含了其他module所要实用的类定义这种情况。在这种情况,别的module使用时会抛错,原因见前面。解决方法是把

此类定义到应用程序domain中。

               
在不同的服务器中加载module

在不同的服务器中加载module,必须在其彼此间建立信任。

跨域访问应用程序

加载应用程序时必须调用allowDomain()方法并且指定你需要加载的module所在的目标domain。因此在应用程序预初始话事件处理时指定目标域来确保程序在module加载前启动。

在module所在的远程服务的cross-domain文件,增加一个entry来指定加载应用程序的服务位置。

在你自己加载的应用程序中的preinitialize事件处理器中加载远程cross-domain文件。

在被加载的module调用allowDomain方法来和调用者通讯。 

 

下面的例子展示了如何在加载应用程序中的init方法

The following example shows the init() method of the loading application:

public function setup():void {

    Security.allowDomain("remoteservername");

    Security.loadPolicyFile("http://remoteservername/crossdomain.xml");

    var request:URLRequest = new URLRequest("http://remoteservername/crossdomain.xml");

    var loader:URLLoader = new URLLoader();

    loader.load(request);

}

下面展示被加载模块的init()方法

public function initMod():void {

    Security.allowDomain("loaderservername");

}

 

下面是cross-domain的写法:

<!-- crossdomain.xml file located at the root of the server -->

<cross-domain-policy>

    <allow-access-from domain="loaderservername" to-ports="*"/>

</cross-domain-policy>

预加载module


 


当你第一次开启应用程序来使用module,此应用程序将会比不使用module的应用程序尺寸更小。同时它也会减小等待时间。但同时,它将会在你访问以前非module的地方


增加延迟。这是因为它不是预加载的。他们在第一次请求的时候被加载。


 


当module第一次被加载时,module的swf文件通过网络传输并储存到浏览器的缓存中。当它被卸载后又被加载时,等待时间会稍短,


这是因为Flash Player将在客户端浏览器缓存中加载它,而非网络。


 


Module的swf文件和其他所有的swf文件一样,存在于客户端缓存一直到用户清空他们。因此,module可以被主应用程序跨不同的session访问,减少


加载时间,但着也依赖于浏览器的缓存清空频率。


你可以预加载module到内存即使当它目前并没有被用到的时候。使用IModuleInfo类的load方法在应用程序启动时可以预加载module。它将module加载到内存,但是并没有创建其实例。


参考下面的例子

    
             
  1. <?xml version="1.0"?>
  2. <!-- modules/PreloadModulesApp.mxml -->
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="preloadModules()">
  4.     <mx:Script>
  5.         <![CDATA[
  6.         import mx.events.ModuleEvent;
  7.         import mx.modules.ModuleManager;
  8.         import mx.modules.IModuleInfo;
  9.         private function preloadModules():void {
  10.             // Get a reference to the module's interface. 
  11.             var info:IModuleInfo = 
  12.                 ModuleManager.getModule("BarChartModule.swf");
  13.             info.addEventListener(ModuleEvent.READY, modEventHandler);
  14.             // Load the module into memory. The module will be
  15.             // displayed when the user navigates to the second
  16.             // tab of the TabNavigator.            
  17.             info.load();
  18.         }
  19.         private function modEventHandler(e:ModuleEvent):void {
  20.             trace("module event: " + e.type); // "ready"
  21.         }
  22.         ]]>
  23.     </mx:Script>
  24.     <mx:Panel 
  25.         title="Module Example" 
  26.         height="90%" 
  27.         width="90%" 
  28.         paddingTop="10" 
  29.         paddingLeft="10" 
  30.         paddingRight="10" 
  31.         paddingBottom="10"
  32.     >
  33.         <mx:Label width="100%" color="blue"
  34.             text="Select the tabs to change the panel."/>
  35.         <mx:TabNavigator id="tn"
  36.             width="100%" 
  37.             height="100%" 
  38.             creationPolicy="auto"
  39.         >
  40.             <mx:VBox id="vb1" label="Column Chart Module">
  41.                 <mx:Label id="l1" text="ColumnChartModule.swf"/>
  42.                 <mx:ModuleLoader url="ColumnChartModule.swf"/>
  43.             </mx:VBox>
  44.             <mx:VBox id="vb2" label="Bar Chart Module">
  45.                 <mx:Label id="l2" text="BarChartModule.swf"/>
  46.                 <mx:ModuleLoader url="BarChartModule.swf"/>
  47.             </mx:VBox>
  48.         </mx:TabNavigator>
  49.     </mx:Panel>
  50. </mx:Application>
使用ModuleLoader事件      
ModuleLoader类会触发很多事件,包括setup, ready, loading, unload, progress, error, 和urlChanged。你可以通过这些事件来记录程序

加载过程,找寻合适module已经被卸载或者何时ModuleLoader目标url已经改变。

 

下面的例子使用定制的ModuleLoader组件,这个组件在主应用程序加载module时对每一个事件都做了一次报告:

定制后的客户端程序

     
              
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <!-- modules/CustomModuleLoader.mxml -->
  3. <mx:ModuleLoader xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" creationComplete="init()">
  4.   <mx:Script>
  5.     <![CDATA[
  6.     public function init():void {
  7.         addEventListener("urlChanged", onUrlChanged);
  8.         addEventListener("loading", onLoading);
  9.         addEventListener("progress", onProgress);
  10.         addEventListener("setup", onSetup);
  11.         addEventListener("ready", onReady);
  12.         addEventListener("error", onError);
  13.         addEventListener("unload", onUnload);
  14.         standin = panel;
  15.         removeChild(standin);        
  16.     }
  17.     public function onUrlChanged(event:Event):void {
  18.         if (url == null) {
  19.             if (contains(standin))
  20.                 removeChild(standin);
  21.         } else {
  22.             if (!contains(standin))
  23.                 addChild(standin);
  24.         }
  25.         progress.indeterminate=true;
  26.         unload.enabled=false;
  27.         reload.enabled=false;
  28.     }
  29.     public function onLoading(event:Event):void {
  30.         progress.label="Loading module " + url;
  31.         if (!contains(standin))
  32.             addChild(standin);
  33.         progress.indeterminate=true;
  34.         unload.enabled=false;
  35.         reload.enabled=false;
  36.     }
  37.     public function onProgress(event:Event):void {
  38.         progress.label="Loaded %1 of %2 bytes...";
  39.         progress.indeterminate=false;
  40.         unload.enabled=true;
  41.         reload.enabled=false;
  42.     }
  43.     public function onSetup(event:Event):void {
  44.         progress.label="Module " + url + " initialized!";
  45.         progress.indeterminate=false;
  46.         unload.enabled=true;
  47.         reload.enabled=true;
  48.     }
  49.     public function onReady(event:Event):void {
  50.         progress.label="Module " + url + " successfully loaded!";
  51.         unload.enabled=true;
  52.         reload.enabled=true;
  53.         if (contains(standin))
  54.             removeChild(standin);
  55.     }
  56.     public function onError(event:Event):void {
  57.         progress.label="Error loading module " + url;
  58.         unload.enabled=false;
  59.         reload.enabled=true;
  60.     }
  61.     public function onUnload(event:Event):void {
  62.         if (url == null) {
  63.             if (contains(standin))
  64.                 removeChild(standin);
  65.         } else {
  66.             if (!contains(standin))
  67.                 addChild(standin);
  68.         }
  69.         progress.indeterminate=true;
  70.         progress.label="Module " + url + " was unloaded!";
  71.         unload.enabled=false;
  72.         reload.enabled=true;
  73.     }
  74.     public var standin:DisplayObject;
  75.     ]]>
  76.   </mx:Script>
  77.   <mx:Panel id="panel" width="100%">
  78.     <mx:ProgressBar width="100%" id="progress" source="{this}"/>
  79.     <mx:HBox width="100%">
  80.       <mx:Button id="unload" 
  81.         label="Unload Module" 
  82.         click="unloadModule()"
  83.       />
  84.       <mx:Button id="reload" 
  85.         label="Reload Module" 
  86.         click="unloadModule();loadModule()"
  87.       />
  88.     </mx:HBox>
  89.   </mx:Panel>
  90. </mx:ModuleLoader>
主应用程序:      
  1. <?xml version="1.0"?>
  2. <!-- modules/EventApp.mxml -->
  3. <mx:Application xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml">
  4.     <mx:Script>
  5.         <![CDATA[
  6.             [Bindable]
  7.             public var selectedItem:Object;
  8.         ]]>
  9.     </mx:Script>
  10.     <mx:ComboBox 
  11.         width="215" 
  12.         labelField="label" 
  13.         close="selectedItem=ComboBox(event.target).selectedItem"
  14.     >
  15.         <mx:dataProvider>
  16.             <mx:Object label="Select Coverage"/>        
  17.             <mx:Object 
  18.                 label="Life Insurance" 
  19.                 module="insurancemodules/LifeInsurance.swf"
  20.             />
  21.             <mx:Object 
  22.                 label="Auto Insurance" 
  23.                 module="insurancemodules/AutoInsurance.swf"
  24.             />          
  25.             <mx:Object 
  26.                 label="Home Insurance" 
  27.                 module="insurancemodules/HomeInsurance.swf"
  28.             />
  29.         </mx:dataProvider>
  30.     </mx:ComboBox>
  31.     <mx:Panel width="100%" height="100%">
  32.         <CustomModuleLoader id="mod" 
  33.             width="100%" 
  34.             url="{selectedItem.module}"
  35.         />
  36.     </mx:Panel>
  37.     <mx:HBox>
  38.         <mx:Button label="Unload" click="mod.unloadModule()"/> 
  39.         <mx:Button label="Nullify" click="mod.url = null"/>
  40.     </mx:HBox>  
  41. </mx:Application>
另一个采用form的例子      
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- modules/insurancemodules/AutoInsurance.mxml -->
  3. <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" 
  4.     layout="absolute" 
  5.     backgroundColor="#ffffff" 
  6.     width="100%" 
  7.     height="100%"
  8. >
  9.     <mx:Label 
  10.         x="147" 
  11.         y="50" 
  12.         text="Auto Insurance" 
  13.         fontSize="28" 
  14.         fontFamily="Myriad Pro"
  15.     />
  16.     <mx:Form left="47" top="136">
  17.         <mx:FormHeading label="Coverage"/>
  18.         <mx:FormItem label="Latte Spillage">
  19.             <mx:TextInput id="latte" width="200" />
  20.         </mx:FormItem>
  21.         <mx:FormItem label="Shopping Cart to the Door">
  22.             <mx:TextInput id="cart" width="200" />
  23.         </mx:FormItem>
  24.         <mx:FormItem label="Irate Moose">
  25.             <mx:TextInput id="moose" width="200" />
  26.         </mx:FormItem>
  27.         <mx:FormItem label="Color Fade">
  28.             <mx:ColorPicker />
  29.         </mx:FormItem>
  30.     </mx:Form>
  31. </mx:Module>
使用error事件


 

error事件用来处理异常。下面的例子:

 

     
              
  1. <?xml version="1.0"?>
  2. <!-- modules/ErrorEventHandler.mxml -->
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
  4.     <mx:Script>
  5.         <![CDATA[
  6.         import mx.events.ModuleEvent;
  7.         import mx.modules.*;
  8.         import mx.controls.Alert;
  9.         private function errorHandler(e:ModuleEvent):void {
  10.             Alert.show("There was an error loading the module." + 
  11.                 " Please contact the Help Desk.");
  12.             trace(e.errorText);
  13.         }
  14.         public function createModule():void {
  15.             if (chartModuleLoader.url == ti1.text) {
  16.                 // If they are the same, call loadModule.   
  17.                 chartModuleLoader.loadModule();
  18.             } else {
  19.                 // If they are not the same, then change the url, 
  20.                 // which triggers a call to the loadModule() method.
  21.                 chartModuleLoader.url = ti1.text;
  22.             }
  23.         }
  24.         public function removeModule():void {
  25.             chartModuleLoader.unloadModule();
  26.         }
  27.         ]]>
  28.     </mx:Script>
  29.     <mx:Panel title="Module Example" 
  30.         height="90%" 
  31.         width="90%" 
  32.         paddingTop="10" 
  33.         paddingLeft="10" 
  34.         paddingRight="10" 
  35.         paddingBottom="10"
  36.     >
  37.         <mx:HBox>
  38.             <mx:Label text="URL:"/>
  39.             <mx:TextInput width="200" id="ti1" text="ColumnChartModule.swf"/>
  40.             <mx:Button label="Load" click="createModule()"/>
  41.             <mx:Button label="Unload" click="removeModule()"/>
  42.         </mx:HBox>
  43.         <mx:ModuleLoader id="chartModuleLoader" error="errorHandler(event)"/>
  44.     </mx:Panel>
  45. </mx:Application>
使用progress事件


 

通过使用progress事件来追踪module加载状态。增加一个progress事件的监听器后,在module的加载时,flex会调用这个监听器。每次它被调用时,你可以

查看这个事件的bytesLoaded属性来判断它的进度百分比属性。


下面的例子:

     
              
  1. <?xml version="1.0"?>
  2. <!-- modules/SimpleProgressEventHandler.mxml -->
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
  4.     <mx:Script>
  5.         <![CDATA[
  6.         import mx.events.ModuleEvent;
  7.         import flash.events.ProgressEvent;
  8.         import mx.modules.*;
  9.         [Bindable]
  10.         public var progBar:String = "";
  11.         [Bindable]
  12.         public var progMessage:String = "";
  13.         private function progressEventHandler(e:ProgressEvent):void {
  14.             progBar += ".";
  15.             progMessage = 
  16.                 "Module " +  
  17.                 Math.round((e.bytesLoaded/e.bytesTotal) * 100) + 
  18.                 "% loaded";
  19.         }
  20.         public function createModule():void {
  21.             chartModuleLoader.loadModule();
  22.         }
  23.         public function removeModule():void {
  24.             chartModuleLoader.unloadModule();
  25.             progBar = "";
  26.             progMessage = "";
  27.         }
  28.         ]]>
  29.     </mx:Script>
  30.     <mx:Panel title="Module Example" 
  31.         height="90%" 
  32.         width="90%" 
  33.         paddingTop="10" 
  34.         paddingLeft="10" 
  35.         paddingRight="10" 
  36.         paddingBottom="10"
  37.     >        
  38.         <mx:HBox>
  39.             <mx:Label id="l2" text="{progMessage}"/>
  40.             <mx:Label id="l1" text="{progBar}"/>
  41.         </mx:HBox>  
  42.         <mx:Button label="Load" click="createModule()"/>
  43.         <mx:Button label="Unload" click="removeModule()"/>
  44.         <mx:ModuleLoader 
  45.             id="chartModuleLoader" 
  46.             url="ColumnChartModule.swf" 
  47.             progress="progressEventHandler(event)"
  48.         />
  49.     </mx:Panel>    
  50. </mx:Application>
你也可以将module连接到一个ProgressBar控件。看下面的例子:      
  1. <?xml version="1.0"?>
  2. <!-- modules/MySimpleModuleLoader.mxml -->
  3. <mx:ModuleLoader xmlns:mx="http://www.adobe.com/2006/mxml">
  4.     <mx:Script>
  5.         <![CDATA[        
  6.             private function clickHandler():void {
  7.                 if (!url) { 
  8.                     url="ColumnChartModule.swf"; 
  9.                 } 
  10.                 loadModule();
  11.             }        
  12.         ]]>
  13.     </mx:Script>
  14.     <mx:ProgressBar 
  15.         id="progress" 
  16.         width="100%" 
  17.         source="{this}"
  18.     />
  19.     <mx:HBox width="100%">
  20.       <mx:Button 
  21.         id="load" 
  22.         label="Load" 
  23.         click="clickHandler()"
  24.       />
  25.       <mx:Button 
  26.         id="unload" 
  27.         label="Unload" 
  28.         click="unloadModule()"
  29.       />
  30.       <mx:Button 
  31.         id="reload" 
  32.         label="Reload" 
  33.         click="unloadModule();loadModule();"
  34.       />
  35.     </mx:HBox>
  36. </mx:ModuleLoader>
  37.  <?xml version="1.0"?>
  38. <!-- modules/ComplexProgressEventHandler.mxml -->
  39. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:local="*">
  40.     <mx:Panel title="Module Example" 
  41.         height="90%" 
  42.         width="90%" 
  43.         paddingTop="10" 
  44.         paddingLeft="10" 
  45.         paddingRight="10" 
  46.         paddingBottom="10"
  47.     >        
  48.         <mx:Label text="Use the buttons below to load and unload    
  49.             the module."/>
  50.         <local:MySimpleModuleLoader id="customLoader"/>
  51.     </mx:Panel>
  52. </mx:Application>
这个例子并不会对所有的事件改变ProgressBar标签的属性,加入你加载了module按后再卸载它,label属性还是保持“LOADING 100%”。

为了调整它的属性,你必须定义其他的ModuleLoader事件处理器,比如unload和error事件。
      

继续阅读