Preloading Modules with ModuleManager

In addition to ModuleLoader, which is a high-level module API, Flex offers ModuleManager. The prime benefit of using ModuleManager is that you can separate the transfer of the module byte code over the network, which is potentially a lengthy operation, from the actual creation of the module instance(s). Certainly, you could do it yourself with the URLLoader (as illustrated in Example 7-1), but you should take advantage of the nice abstraction layer provided by the ModuleManager class. In particular, the contract of the ModuleManager guarantees that you won’t transfer the module bytes over the network more than once.

To load a module into a singleton registry of modules provided by ModuleManager, you use a module proxy, such as an implementation of the IModuleInfo interface, corresponding to the module URL. You then perform the load() via this module proxy, as shown in Example 7-7. The actual loading task will be delegated to flash.display.Loader.

Example 7-7. Module preloading technique

private var moduleInfoRef:Object = {}; private function loadModule(moduleUrl:String):void { var moduleInfo:IModuleInfo = ModuleManager.getModule(moduleUrl); moduleInfo.addEventListener(ModuleEvent.READY, onModuleReady ) ; //You need to protect moduleInfo from being garbage-collected moduleInfoRef[moduleUrl] = moduleInfo; moduleInfo.load(); } // Module is loaded. You may create modules via event.module.factory private function onModuleReady(event:ModuleEvent):void ...

Get Agile Enterprise Application Development with Flex now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.