Skip to content

Instantly share code, notes, and snippets.

@a-r-d
Created June 22, 2012 02:58
Show Gist options
  • Save a-r-d/2969933 to your computer and use it in GitHub Desktop.
Save a-r-d/2969933 to your computer and use it in GitHub Desktop.
Custom event dispatching example Flash Builder 4.6
package myEvents
{
import flash.events.Event;
public class CustomAwesomeEvent extends Event
{
public static const ENABLE_CHANGED:String = "enableChanged";
public function CustomAwesomeEvent(type:String, isEnabled:Boolean=false)
{
// you have to do this for some reason
super(type);
// Set the new property.
this.isEnabled = isEnabled;
}
// Define a public variable to hold the state of the enable property.
public var isEnabled:Boolean;
// Override the inherited clone() method.
override public function clone():Event {
return new CustomAwesomeEvent(type, isEnabled);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="SingleMessageDisplayer"
contentCreationComplete="view1_contentCreationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import myEvents.CustomAwesomeEvent;
protected function goBack_clickHandler(event:MouseEvent):void
{
navigator.popToFirstView();
}
protected function button1_clickHandler(event:MouseEvent):void
{
//custom event
dispatchEvent(new CustomAwesomeEvent("enableChanged", true));
trace("event was just dispatched");
}
protected function view1_contentCreationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
addEventListener(CustomAwesomeEvent.ENABLE_CHANGED, doSomethingCool);
trace("Added listener on content creation complete");
}
protected function doSomethingCool(e:CustomAwesomeEvent):void{
trace("fired do something cool");
trace("Type:" + e.type);
trace("Is enabled?" + e.isEnabled);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button id="goBack" x="123" y="311" label="Back" click="goBack_clickHandler(event)"/>
<s:Label x="10" y="20" text="Time:"/>
<s:Label x="76" y="20" width="219" text="{data.date}"/>
<s:Label x="10" y="43" text="Id:"/>
<s:Label x="76" y="43" width="235" text="{data.id}"/>
<s:Label x="10" y="67" text="Source:"/>
<s:Label x="76" y="66" width="234" text="{data.source}"/>
<s:Label x="10" y="109" text="Message:"/>
<s:Label left="10" top="130" width="300" height="162" text="{data.text}"/>
<s:Button x="229" y="27" width="81" height="31" label="Custom Event"
click="button1_clickHandler(event)" fontSize="7"/>
</s:View>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment