Passing Data With Custom Event
Posted by shardulbartwal on December 3, 2007
As being an event driven language Action Script provides the mechanism of sending any kind of object with the event.The concept is more important when we are supposing to get value of any varialbe of mxml into any another mxml which is far enough in the structure(Folder Structure)of our Application.It is very simple to create any custom event for the occurence of any particular event.Within the same parent folder in which your mxml containing the variable create a folder (let suppose with name ‘event’).In this mxml let suppose you are having a variable of arrayCollecton type its name is ‘myArrayCollection’ and it is already having some value.
Now you want to get this variable in any another mxml which is far from this.Then for doing this make one custom event ‘GetMyArrayCollectionEvent’ in the folder where your mxml(which is having that arrayCollection)is present.
Just create one action Script class file Copy and Paste the code below.
——————————————————————————————–
package myPro.events
{
import flash.events.Event;
public class GetMyArrayCollectionEvent extends Event
{
public var mybool:Boolean=true;
public var myArrayCollection:ArrayCollection;
public function PropertyThumbEvent (type:String,myArrayCollection,mybool:Boolean=true):void
{
super(type);
this.myArrayCollection = myArrayCollection;
this.b=b ;
}
override public function clone():Event
{
return new GetMyArrayCollectionEvent(type,myArrayCollection,mybool);
}
}
}
——————————————————————————————–
Now at this mxml where ‘myArrayCollection’ is present First add Metadata as below :-
——————————————————————————————–
<mx:Metadata>
[Event(name="myCustomEvent",type="myPro.events.GetMyArrayCollectionEvent")]
</mx:Metadata>
——————————————————————————————–
Again from the same mxml just dispatch one event i.e. as:-
——————————————————————————————–
dispatchEvent(new GetMyArrayCollectionEvent(“myCustomEvent”,myArrayCollection,true));
——————————————————————————————–
One thing noticable here is that here ‘myCustomEvent’ is the name of event which can be any variable name,and ‘GetMyArrayCollectionEvent’ is the type of the event.
Now on the mxml where you want to get this variable just get the event and with this event you will find ‘myArrayCollection’.You can just see it by tracing i.e.
trace(event.myArrayCollection).
Now you can do any thing with this collection as per your requirement.
Enjoy Flexing………………













Powers Smith said
I’m a newbie to flex and object oriented programming and I’m trying to duplicate your code for passing a value from a child module to a parent module. I am only trying to pass an integer value. Below is my code, but when I add the ‘dispatchEvent’ line, I get an error 1093: Syntax error, but I cannot see what is wrong. Any advise will be appreciated. The class file, and both mxml files are all in the same directory folder (com.olt.transportation.view).
My class:
package com.olt.transportation.view
{
import flash.events.Event;
public class GetMyIntValueEvent extends Event
{
public var mybool:Boolean=true;
public var myIntValue:int;
public function PropertyThumbEvent (type:String,myIntValue,mybool:Boolean=true):void
{
super(type);
this.myIntValue = myIntValue;
this.b=b ;
}
override public function clone():Event
{
return new GetMyIntValueEvent(type,myIntValue,mybool);
}
}
}
My metadata:
[Event(name="myCustomEvent",type="com.olt.transportation.view.GetMyIntValueEvent")]
My dispatch line:
dispatchEvent(new GetMyIntValueEvent(”myCustomEvent”,TAyValue,true));
where TAyValue is the value that I want passed to the parent module
amarshukla said
I don’t understand what’s happenign here at this line..why it is used -
this.b=b ;
Can you explain .. if its typo ?
shardulbartwal said
hi…….
sorry it get changed….
it is like :-
this.mybool = mybool ;
Thnx for update……..
with Regards,
Shardul Singh Bartwal