Welcome to my blog! Feel free to post comments.
- Andrew


Visit Coldfusion Community


<< April, 2008 >>
SMTWTFS
12345
6789101112
13141516171819
20212223242526
27282930
Search Blog

Categories
Archives
RSS


Powered by
BlogCFM v1.14

Dispatching custom events from a Flex Module
29 April 2008

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

Posted by aschwabe at 1:04 PM | Link | 0 comments


DRM: Spiral Frog + AnalogWhole + Itunes = free music for ipod and iphone
20 April 2008
There have been some nice new "advertiser sponsored" free music sites to pop up over the last few years.  One of the most promising is Spiral Frog (http://www.spiralfrog.com).  The downside to pretty much all of them has been that songs downloaded from these sites are digital rights managed (DRM) which is used to ensure that only licensed users can listed to the files.  Basically once you download one of these songs, you can't email it to your friend and let them listen -- they will get a license error.

This technology for digital rights management is essential so that the already struggling music industry doesn't lose what little money it is making.  It used to make a killing on CD sales, but not anymore thanks to illegal music distribution online.

So, the upside to DRM is that you can't give illegal copies to your friends.  The downside is that if you use a service like Spiral Frog (which uses Microsoft Windows Media Player for DRM and playback), then you cannot put those files on your iPod or iPhone.  Hey, the technology isn't perfect... YET.

There is a solution! (you knew i was going there, right ?)  This requires you to be a morally responsible person however, and any "gray" areas on this are up to you to decide on.  I am not, in any way, endorsing this information, I am only providing it as a public service.

There is a nifty open source product called AnalogWhole (http://analogwhole.com) which removes DRM from your windows media audio files (it doesn't support video at this time).  Once you configure AmalogWhole, it will watch your incoming folder for new music files, and automatically convert them to plain MP3 files _AND_ add them to your iTunes music library (f you want).

AmalogWhole isn't a perfect program yet -- it actually plays back the original, and does a digital recording to a new audio file, so the speed is 1-to-1, your computer has to play back each file.  It is a little quirky in that your volume level of your speakers (from within windows) affects the recording level, so it takes a little experimentationi.  After all the effort however, what you end up with is a clean MP3, automatically added to your iTunes library, ready to sync to your iPod and it sounds pretty good.

Once you have a process down like this, it allows you to take advantage of advertiser-sponsored free music sites like SpiralFrog, and then also add that music to your portable audio player, just as we _should_ be able to.  All it requires is discipline to not distribute those unprotected audio files, and thus making the music industry we all love collapse on itself.  You can do that right ?

Ciao, and stay LEGAL !
Posted by aschwabe at 12:00 AM | Link | 0 comments