Skip to main content

Dispatching custom events from a Flex Module

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

Popular posts from this blog

Installing python 3.4.x on OSX El Capitan

I love "brew" package manager, but sometimes being too progressive breaks things.  I have several python apps that I maintain that get deployed to AWS using Elastic Beanstalk.  AWS eb can deploy with python 2.7 or 3.4.  Any recent 'brew install python3" will get 3.5.1. #annoying

Making Macbook Air with 128GB SSD usable with Bootcamp

I recently got a new Macbook Air 11" (the 2012 version) and loaded it with goodies like 8GB ram and 2GHz Core i7.  What I DIDN'T upgrade was the internal SSD.  My config came with 128GB SSD and I refused to pay $300+ to upgrade it to 256GB.  Yeah I know, some call me cheap, but SSds cost $75-$150 for 240GB, so adding another 128GB for $300 seemed way too steep for me.  I figured "ok, I'm going to make 128G work!"

Here is the story of how that went...

Election day plus 1: Social Media Nightmare

It is election day plus 1, and a very emotional day with no way to win for many.

Social media was a force to be reckoned with this election year.  No matter what position you take, the world is ready to pounce on you and tell you why YOU are evil, intolerant or a deplorable -- and the reason for all their woes.  This isn't a new problem, though...  All of the liberal vs conservative issues are causing rifts between otherwise civil people, and it seems to be the worst on Facebook and twitter.

If I say I am for Hillary:
i am part of the "corrupt system."

If I say I am against Hillary:
I am stupid and uneducated

If I say I am for Trump:
I am against women,
I am religiously intolerant,
I am a bigot

If I say I am against Trump:
I am against the 2nd amendment,
I don't have any idea how government works,
I am part of the problem with American society,
I am against the police

Stay with me now, this isn't meant to make people angry, but it is mean to shine some light on a social …