You can't pass parameters using dispatchEvent in As3 so the best way to do it is to create a custom class that extend the Event class.
You'll dispatch the new event in this way:
var e:CustomEvent = new CustomEvent(CustomEvent.FORM_SUBMITTED, nameTxt.text, surnameTxt.text);
this.dispatchEvent(e);Following the CustomEvent class:
package events
{
import flash.events.Event;
public class CustomEvent extends Event
{
public static const FORM_SUBMITTED:String = "FormSubmitted"
public var name:String;
public var surname:String;
public function CustomEvent( _type:String, _userName:String, _surName:String):void
{
super(_type);
this.name = _userName;
this.surname = _surName;
}
override public function clone():Event
{
return new CustomEvent(type, name, surname);
}
}
}
this isn't an unpublished info for sure, but can be useful as a little reminder.
The scenario:
You have some custom mxml event and need to add your custom event o simply dispatch an ordinary event not listed in your parent class. Usually extending mxml comp it's a matter to base on IEventDispatcher inherited class, so it's quite easy to invoke dispatchEvent BUT ...
You want to refer to you component in mxml code, f.i.: change="onCustomCompChange(event)"
the solution:
- add the event you need via Metadata tag
- of course, if it's a custom event, you must implement it :D
in this snippet we simple add Event.CHANGE to an HBox (that's current missing by default)
// your custom comp
<?xml version="1.0" encoding="UTF-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Metadata>
[Event(name="change", type="flash.events.Event")]
</mx:Metadata>
<mx:Script>
<![CDATA[
import flash.events.Event;
import flash.events.MouseEvent;
/*... */
/**
* event that dispatch the event we added
*/
internal function onBtClickmeClick(evt:MouseEvent):void {
if(hasEventListener(Event.CHANGE)) dispatchEvent(new Event(Event.CHANGE, true));
}
]]>
</mx:Script>
<mx:Button label="clickme" click="onBtClickmeClick(event)" />
</mx:HBox>
// your app demo, fit the path to your CustomComp
<?xml version="1.0" encoding="UTF-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
ui="path.to.your.customcomp.*">
<mx:Script>
<![CDATA[
import flash.events.Event;
import flash.events.MouseEvent;
/*... */
/**
* change listener on customComp
*/
internal function onCustomCompChange(evt:Event):void { trace("something has changed!"); }
]]>
</mx:Script>
<ui:CustomComp change="onCustomCompChange(event)" />
</mx:Application>
/*
:)
Jaco
*/
I was looking to a way to dispatch events between ultiple windows in an AIR application and, after I found on google only some solution that state to send a reference of the target for the event to the new window uing public properties, I came out with a more elegant solution that involves a new class added to nabiro.
The idea is that this class acts like a bridge on which is possible to register a DisplayObjetc for a specific message (i.e. the event type), the snippet represent only the simple usage of thebridge for register a new DisplayObject, attached there is the complete sample.
bridge = WindowsBridge.getInstance();
bridge.register(this, "Test");
addEventListener("Test", onTest);
// Window dispatch code
var test:WindowsBridge = WindowsBridge.getInstance();
test.dispatchEvent(new IntraWinComEvent(IntraWinComEvent.DEFAULT, new Event("Test")));