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………………











