IFlexModuleFactory – create and examine loaded Flex Modules
In an earlier post I described how you can load in Flex modules using the ModuleManager class and the IModuleInfo interface, but I didn’t really mention IFlexModuleFactory which is pretty fundamental to the whole process.
The factory is responsible for knowing how to create an instance of the module, and also provides access to certain properties of the module. It can get a little more complicated as the methods exposed by the factory become available at different stages of the load process, so you have to be careful not to try and retrieve information before it is available.
The factory is retrieved used by accessing the factory property of the IModuleInfo object, returned by ModuleManager.getModule (). For more information on this, see the previous post. The factory will be of type IFlexModuleFactory, which behind the scenes is implemented by mx.managers.SystemManager. You don’t really need to worry about this detail though, it’s enough to know about the two public methods exposed by the factory.
They are:
- IFlexModuleFactory.create – This is really the most important method, and provides the mechanism for creating an instance of your loaded module. When enough of the module has been downloaded for this method to be called, the IModuleInfo object will dispatch a ModuleEvent.READY event, and the public property IModuleInfo.ready will return true. The method allows you to optionally pass in one or more parameters, using the “…rest” mechanism. These parameters are passed to your module’s constructor. Calling this method will create a new instance of your module, and the new instace of the method will be returned. The return type is specified as Object, so you will need to manually cast the returned object to the correct type in order to work with it.
- IFlexModuleFactory.info – This module allows you to find out a bit of information about the module which is being loaded. When enough of the module has been downloaded for this method to be called, the IModuleInfo object will dispatch a ModuleEvent.SETUP event, and the public property IModuleInfo.setup will return true. The method returns an Object, containing a variety of name/value pairs providing bits of information about the module. Examples of the kind of information that can be contained in the object includes width, height, a list of mixins used by the module, a list of RSLs used by the modules, a list of fonts embedded within the module, and information about the modules Main class name. The method can be useful for finding out the module’s layout information before you create an instance of it and add it to the display list.
Just to mention a quick ‘gotcha’ that I encountered when first working with Modules… If you find that the call to IModuleData.factory.create for some reason returns null, or at some point in the process you get an Error #1009, most likely your module is using some classes which for some reason are not accessible once the SWFs have been compiled and optimised. A simple fix for this can be to reference the offending classes in some way in the main application to force them to be compiled. Common offenders are the ‘manager’ classes, for example DragManager, PopupManager and SystemManager.
Tags: Flex, Modules