Want to send parameters with custom dispatch event - actionscript-3

I am creating a library. Here is an example
[Event (name="eventAction", type="something")]
public function create_new_customer(phone_number:String):void
{
-------------;
----;
------------;
rpc.addEventListener(Event.COMPLETE, onCreate_returns);
}
private function onCreate_returns(evt:Event):void
{
var ob:Object = evt.target.getResponse();
dispatchEvent(new something("eventAction"));
}
I have a listener to this event in app side. So when I manually dispatch event I want the
"ob" to be sent as a parameter. How to do it?

You need to create a custom event class with extra properties to pass data with it. In your case you could use a class like
public class YourEvent extends Event
{
public static const SOMETHING_HAPPENED: String = "somethingHappend";
public var data: Object;
public function YourEvent(type:String, data: Object, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.data = data;
}
override public function clone():Event
{
return new YourEvent (type, data, bubbles, cancelable);
}
}
then when yo dispatch you do:
dispatchEvent(new YourEvent(YourEvent.SOMETHING_HAPPENED, ob));

In AS3 you can use DataEvent:
ex:
dispatchEvent( new DataEvent(type:String[,bubbles:Boolean=false,cancelable:Boolean=false, data:String ] );
Instead of example data, I showed the parameters DataEvent takes.
I hope this helps.
Best regards, RA.

Make your custom event carry this ob object. Pass it to the custom event's ctor and voila!

Related

Flex actionscript, get data from AsyncToken call

I have Flex/Java project with blazeDS. Now I have an actionscript file that call a method of another actionscript that call the remoteObject (java class who make a simple select on db)
Here's the code:
Home.as
..
private var _dm:DataManager = new DataManager;
public function getPerson():void { // this is connect to a button in .mxml
_dm.getPerson();
}
..
DataManager.as
public class DataManager {
private var _service:RemoteObject;
private var _url:URLRequest;
private var loCs:ChannelSet = new ChannelSet();
public function DataManager () {
_service = new RemoteObject("PeopleDAO");
loCs.addChannel(new AMFChannel("canale", "http://localhost:8080/FlexTRYOUT/messagebroker/amf"));
_service.channelSet = loCs;
}
private function onFault(event:FaultEvent,token:Object):void {
var _fail:String = "fault";
}
private function onResult(event:ResultEvent,token:Object):void {
per = event.result as People; // is a bean class
Alert.show(per.nome);
}
public function getPerson():void {
var token:AsyncToken = _service.getPersona();
token.addResponder(new AsyncResponder(onResult,onFault));
}
}
The call works fine, it calls java method names getPerson() of the DataManger.java class. It return simply one object with name and surname (it's just a hello world to understand this damned AsyncCall). The problem is that I don't know how send this result to Home.as with a classic (java) return type. I have the result in onResult method and I don't know how to get it.
I try to follow Brian instructions and I just waste my time. Maybe because I'm not a flex actionscript programmer but I added the code Brian posted and:
public function getPerson():void { // this is connect to a button in .mxml
_dm.addEventListener(DATA_RECEIVED, onPersonFound); * compile error 1
_dm.getPerson();
}
error is DATA_RECEIVED is undefined
than in DataManager:
public class DataManager {
public static const DATA_RECEIVED:String = "DATA_RECEIVED";
...
private function onResult(event:ResultEvent,token:Object):void {
per = event.result as People; // is a bean class
dispatchEvent(new DataReceivedEvent(DATA_RECEIVED, per)); * compile error 2
}
}
error 2 is call of possible undefined method dispatchEvent
Where is the mistake? Please guys write the complete code because I'm on flex - actionscript - blazeds from two days and I have a few time to try solution. Thanks
OK, Sorry for all this post, I just create new one (but more elaborated and clear) with the same question. Step by Step I'm studing this language and I manage to implement the Brian code but DataManager.as class must extend EventDispatcher, if I don't extend this I have the compile error I posted. At moment I mangage to obtain the resultEvent data in the method defined in the addEventListener call (onPeopleFound in this case). Thanks a lot Brian I think I surely need your help again in future (at least until acceptance of the project). Bye
You can adjust method getPerson to have two parameters referencing the callback functions.
public function getPerson(onResultCallback:Function, onFaultCallback:Function):void {
var token:AsyncToken = _service.getPersona();
token.addResponder(new AsyncResponder(onResultCallback,onFaultCallback));
}
This way you can receive data in an instance of the class you need.
One option is to dispatch an event when you get the data back from the Java call:
Home.as
...
public function getPerson():void { // this is connect to a button in .mxml
_dm.addEventListener(DATA_RECEIVED, onPersonFound);
_dm.getPerson();
}
private function onPersonFound(dataEvent:DataReceivedEvent):void {
var person:People = dataEvent.people;
//Do important processing...
}
...
In DataManager.as
public class DataManager {
public static const DATA_RECEIVED:String = "DATA_RECEIVED";
...
private function onResult(event:ResultEvent,token:Object):void {
per = event.result as People; // is a bean class
dispatchEvent(new DataReceivedEvent(DATA_RECEIVED, per));
}
}
And DataReceivedEvent.as will look like the answer to How to dispatch an event with added data - AS3
public class DataReceivedEvent extends Event
{
public static const DATA_RECEIVED:String = "DATA_RECEIVED";
// this is the object you want to pass through your event.
public var result:Object;
public function DataReceivedEvent(type:String, result:Object, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.result = result;
}
// always create a clone() method for events in case you want to redispatch them.
public override function clone():Event
{
return new DataReceivedEvent(type, result, bubbles, cancelable);
}
}

How to dispatch an event with added data - AS3

Can any one give me a simple example on how to dispatch an event in actionscript3 with an object attached to it, like
dispatchEvent( new Event(GOT_RESULT,result));
Here result is an object that I want to pass along with the event.
In case you want to pass an object through an event you should create a custom event. The code should be something like this.
public class MyEvent extends Event
{
public static const GOT_RESULT:String = "gotResult";
// this is the object you want to pass through your event.
public var result:Object;
public function MyEvent(type:String, result:Object, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.result = result;
}
// always create a clone() method for events in case you want to redispatch them.
public override function clone():Event
{
return new MyEvent(type, result, bubbles, cancelable);
}
}
Then you can use the code above like this:
dispatchEvent(new MyEvent(MyEvent.GOT_RESULT, result));
And you listen for this event where necessary.
addEventListener(MyEvent.GOT_RESULT, myEventHandler);
// more code to follow here...
protected function myEventHandler(event:MyEvent):void
{
var myResult:Object = event.result; // this is how you use the event's property.
}
This post is a little old but if it can help someone, you can use DataEvent class like so:
dispatchEvent(new DataEvent(YOUR_EVENT_ID, true, false, data));
Documentation
If designed properly you shouldn't have to pass an object to the event.
Instead you should make a public var on the dispatching class.
public var myObject:Object;
// before you dispatch the event assign the object to your class var
myObject = ....// whatever it is your want to pass
// When you dispatch an event you can do it with already created events or like Tomislav wrote and create a custom class.
// in the call back just use currentTarget
public function myCallBackFunction(event:Event):void{
// typecast the event target object
var myClass:myClassThatDispatchedtheEvent = event.currentTarget as myClassThatDispatchedtheEvent
trace( myClass.myObject )// the object or var you want from the dispatching class.

AS3 CustomEvent not being extended at all?

I get some weird errors when creating CustomEvent, it appears Event being extended does not give access to Event properties:
package
{
import flash.events.Event;
public class CustomEvent extends Event
{
//public static const COMPLETE:String = 'complete';
private var _assetName:String;
public function get assetName ():String
{
return _assetName;
}
public function set assetName ( aname:String ):void
{
_assetName = aname;
}
public function CustomEvent (type:String, bubbles:Boolean = false, cancelable:Boolean = false)
{
super (type, bubbles, cancelable);
}
public override function clone ():Event
{
return new CustomEvent(type, bubbles, cancelable) as Event;
}
}
}
When doing:
myObj.addEventListener(CustomEvent.COMPLETE, objLoaded);
I get error that COMPLETE doesnt exist... Ok, i set it to the place and then it caomplains about cannot convert CustomEvent to Event.
What am i missing here ??
You need to declare this public static const COMPLETE:String = 'complete'; as static var/const are not transfered to extending classes.
The error about converting CustomEvent to Event may be caused by setting event listener to listen to the Event not CustomEvent.
Where is the event dispatched and what does the signature of the listener look like?
We've got a few issues to cover here:
You have your public static const COMPLETE... commented out. Why? That's needed if you want to refer to CustomEvent.COMPLETE as the event type.
Using my psychic third eye, you've got your event listener declared like this:
public completeListener(evt:Event):void
...
That's not going to work the way you want it to. You need
public completeListener(evt:CompleteEvent):Void
...
Unrelated to the compilation issue, your custom event has another issue. Your clone method is not going to clone the assetName property. Try something like this:
public override function clone():Event
{
var ret:CustomEvent = new CustomEvent(type, bubbles, cancelable);
ret.assetName = assetName;
return ret;
}

How do I handle a custom event in ActionScript 3?

I've created an Event Handler/Listener like so:
import flash.events.Event;
public class DanielEvent extends Event {
public var data:*;
public static const APP_STARTED:String = "APP_STARTED";
public function DanielEvent(n:String, data:*){
this.data = data;
super(n)
}
}
Listening to an event using:
addEventListener(DanielEvent.APP_STARTED, appStarted);
Dispatching an event by:
dispatchEvent(new DanielEvent("APP_STARTED", "test"))
And receiving the data by:
private function appStarted(e:Event){
trace(e.data)
}
But I get the error:
Access of possibly undefined property
data through a reference with static
type flash.events:Event.
You have to use your custom event type in the event handler, if you want to access the data property:
private function appStarted(e:DanielEvent): void {
trace(e.data);
}
your event handler is passed a DanielEvent, not an Event:
private function appStarted(e:DanielEvent):void
{
trace(e.data);
}
also. you should also use your constant for your dispatch instead of passing a string, like you've done for your listener:
dispatchEvent(new DanielEvent(DanielEvent.APP_STARTED, "test"));
and don't forget to override clone() if you are planning on dispatching that event more than once.
public override function clone():Event
{
return new DanielEvent(n, data);
}

SWFAddress CHANGE Event, dispatch Object

Is there a way to dispatch a SWFAdresss CHANGE Event but also pass parameters (an Object) along with it?
I see something like that in the documentation but I can't find an example online...
You could modify the SWFAddressEvent class like so:
private var _customObject:Object;
public function SWFAddressEvent(type:String, customObject:Object, bubbles:Boolean = false, cancelable:Boolean = false) {
super(type, bubbles, cancelable);
_customObject = customObject;
}
and then when you dispatch the CHANGE event, add the object to the Event:
dispatchEvent(SWFAddressEvent.CHANGE, customObject);
To make the Object publically available:
public function get publicCustomObject():Object
{
return _customObject;
}