Passing parameters to a module
modular is made for creating and changing the content by editing the boot.xml so its not surprising that the passing of data from xml to an actionscript instance is very easy. There are two ways to pass data from the xml to the module:
Public property
Create a public property on the actionscript side and you can set its value from xml as long its a Number, String or a Boolean. This is usefull for simple data like an url, flag and so on. Here is an example where we pass an image url to the modules.Image module:
The image module
package modules {
import flash.display.Loader;
import flash.net.URLRequest;
import modular.core.module.DisplayModule;
public class Image extends DisplayModule {
private var loader:Loader;
public function Image() {
super();
loader = new Loader();
addChild( loader );
}
/**
* these is the public property
*/
public function set url( value:String ):void {
loader.load( new URLRequest( value ) );
}
}
}
according boot.xml
<?xml version="1.0" encoding="UTF-8"?> <ModularApplication> <Page> <Assets> <Library path="swf/ProjectLibrary.swf" /> </Assets> <Modules> <Module class="modules.Image" url="path/to/an/image" /> </Modules> </Page> </ModularApplication>
raw xml
If you have more complex data to pass to a module you can access its fully configuration xml. In the following example we create a navigation module that should store multiple pages:
navigation module class
package modules {
import modular.core.module.DisplayModule;
public class Navigation extends DisplayModule {
private var pages:XMLList;
public function Navigation(){
super();
}
public override function init():void {
// here we get the fully configuration xml: <Module class="..">....</Module>
// but we store just the list of the pages.
pages = configuration.rawXML.Page;
}
}
}
according boot.xml
<?xml version="1.0" encoding="UTF-8"?> <ModularApplication> <Page> <Assets> <Library path="swf/ProjectLibrary.swf" /> </Assets> <Modules> <Module class="modules.Navigation"> <Page> <Modules> <Module class="modules.Text" /> </Modules> </Page> <Page> <Modules> <Module class="modules.Image" /> </Modules> </Page> </Module> </Modules> </Page> </ModularApplication>
Comments