Accessing modules

modular provides an easy to use set of functions to get every module in a page that is displayed/loaded in a pageloader. So if you have for example a boot.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<ModularApplication>
	
	<Page>
		<Assets>
			<Library path="swf/ProjectLibrary.swf" />
		</Assets>
		<Modules>
			<Module class="modules.Text" />
			<Module class="modules.Image" />
			<Module class="modules.Text" id="myText" />
		</Modules>
	</Page>
	
</ModularApplication>

You can retrieve for example the modules.Image instance by calling:

var img:Image = Modular.getModule( "modules.Image" ) as Image;
// or
var img:Image = Modular.getModuleByClass( Image );

The later is prefered as this will display an error when renaming or moving the module in another package.

There is a problem with this approach when you have multiple instances of the same class like the modules.Text in the example above. But in this case you can specify an id for the module in the xml and you get the specified module. The next example shows this usage:

var txt:Text = Modular.getModule( "myText" ) as Text;

tl_files/modular_template/help_hint.pngIf you have singleton classes like a backend communication module you can create a static method that retrieves the module for you like this:

public static function get instance():Singleton {
return Modular.getModuleByClass( Singleton );
}

Comments

*
*
Please add 4 and 4.*