I have been working to make my Flex applications more and more modular. Flex now supports modular development with the ModuleManager so you can dynamically load and unload modules at runtime.
There is reasonably good documentation online for how to load and unload basic modules, and even how to set public vars and call public methods defined in modules after they are loaded. There were not, however, any examples (that i could find anyway...) for having a module dispatch a custom event class, so that your application can catch and respond to the event.
Here is a basic outline of an app, a custom event class that stores some bits of information, and a module that demonstrates dispatching the custom event class:
The main Flex app "SimpleModuleEvents.mxml" :
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="initApp()"
layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import net.schwabe.demos.CustomEvent;
import mx.modules.*;
import mx.events.ModuleEvent;
public var myModuleInfo:IModuleInfo;
private function initApp():void
{
// load our module (but don't display yet)
myModuleInfo = ModuleManager.getModule("modules/Dispatcher.swf");
myModuleInfo.addEventListener(ModuleEvent.READY, displayModuleHandler);
myModuleInfo.load();
// start listening for our custom event.
addEventListener("customEvent", handleCustomEvent);
}
/* Runs when the module is finished loading and is ready to display */
private function displayModuleHandler(event:ModuleEvent):void
{
myPanel.addChild(myModuleInfo.factory.create() as DisplayObject);
}
/* runs when the custom event is caught */
private function handleCustomEvent(event:CustomEvent):void
{
Alert.show("received event with data: " + event.myNumber + "=" + event.myString);
}
]]>
</mx:Script>
<mx:Panel id="myPanel" width="300" height="300">
</mx:Panel>
</mx:Application>
The module "Dispatcher.mxml" :
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Script>
<![CDATA[
import net.schwabe.demos.CustomEvent;
private function doEvent():void
{
var myEvent:CustomEvent = new CustomEvent('customEvent',42,'The answer to all life and everything');
parentDocument.dispatchEvent(myEvent);
}
]]>
</mx:Script>
<mx:HBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<mx:Button label="Dispatch CustomEvent!" click="doEvent()"/>
</mx:HBox>
</mx:Module>
The custom event class:
package net.schwabe.demos
{
import flash.events.Event;
public class CustomEvent extends Event
{
public var myNumber:Number;
public var myString:String;
// constructor
public function CustomEvent(type:String, myNumber:Number, myString:String)
{
super(type);
this.myNumber = myNumber;
this.myString = myString;
} // end constructor
public override function clone():Event
{
return new CustomEvent(type, myNumber, myString);
}
}
}
Hope this is helpful, as it is a very bare bones explanation of how to modularize your code and use custom event classes to pass around useful data.
Ciao