How to dispatch event from a class

79 views
Skip to first unread message

riaflexible

unread,
Nov 21, 2008, 5:46:36 AM11/21/08
to Flex India Community
Hi ,

Is it possible to dispatch event from a class .Please suggest me ways
to do it .

Thanks,
Satish

nagu

unread,
Nov 21, 2008, 7:23:11 AM11/21/08
to Flex India Community
Ya dude you can send by Extending your class by DispatchEvent and
public class Handler extends EventDispatcher
{
public static const RECORDS_FOUND:String="DONE";
public var err:String=null;
public function JsonResultHandler()
{

}
[Bindable] public var resp:ArrayCollection;
public function playerHandler(event:ResultEvent):void
{
var rawresp:String=String(event.result);
dispatchEvent(new Event(JsonResultHandler.RECORDS_FOUND));
}

}
}

and then in your component or application
var obj:Handler=new Handler();
obj.addEventListener(Handler.RECORDS_FOUND,yourfunction);
function yourfunction():void{

riaflexible

unread,
Nov 24, 2008, 2:18:42 AM11/24/08
to Flex India Community
Hi ,

Thanks for the solution , Still i am not able to get through .

I created a class by extending Event Dispatcher .

When i dispatch event from this class , The Main Application Mxml , I
recieving the event .

But i want another class to listen to the event , So i instantiate the
class which dispatches the event , And decalred the add event
listener , But i am not able to listen to the event .

Please guide ways to achieve it .

vengu

unread,
Nov 24, 2008, 4:43:56 AM11/24/08
to Flex India Community
Have Class1 extend EventDispatcher

Create Class 2 .

In a method in class 2

Have something like

Class1 ob1= new CLass1();
ob1.addEventListener("EventName",secondMethod);


Make sure you have the correct event name.

Regards,
Venkat
http://www.techmytongue.blogspot.com/

On Nov 21, 3:46 pm, riaflexible <riaflexi...@gmail.com> wrote:

riaflexible

unread,
Nov 24, 2008, 5:38:48 AM11/24/08
to Flex India Community
I tried but i am not getting it , Please see the below code ,



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
<![CDATA[

public function init():void
{
var classa:ClassA = new ClassA();
var classb:ClassB = new ClassB();
classb.announceEvent();
}

]]>
</mx:Script>

<mx:Button id="btn" label="Check" click="init()"/>
</mx:Application>


package
{
import mx.controls.Alert;

public class ClassA
{
public function ClassA()
{
var classb:ClassB = new ClassB();
classb.addEventListener(MyEvent.CHECK,handleIt);
}

public function handleIt(event:MyEvent):void
{
mx.controls.Alert.show(' Fingers crossed ');
}

}
}



package
{
import flash.events.EventDispatcher;

public class ClassB extends EventDispatcher
{
public function ClassB()
{

}

public function announceEvent():void
{
this.dispatchEvent(new MyEvent(MyEvent.CHECK));
}

}
}



package
{
import flash.events.Event;

public class MyEvent extends Event
{
public static var CHECK:String ="CHECK";

public function MyEvent(type:String):void
{
super(type);
}

}
}


Please let me know where i have made mistake


On Nov 24, 2:43 pm, vengu <venc...@gmail.com> wrote:
> Have Class1 extend EventDispatcher
>
> Create Class 2 .
>
> In a method in class 2
>
> Have something like
>
> Class1 ob1= new CLass1();
> ob1.addEventListener("EventName",secondMethod);
>
> Make sure you have the correct event name.
>
> Regards,
> Venkathttp://www.techmytongue.blogspot.com/

riaflexible

unread,
Nov 24, 2008, 7:41:50 AM11/24/08
to Flex India Community
Any ideas .

Venkat Viswanathan

unread,
Nov 24, 2008, 12:01:57 PM11/24/08
to flex_...@googlegroups.com
Hi,

The way you are implementing the functionality is incorrect. You are adding the event listener to the classb variable inside ClassA. And you are calling the announceEvent() method from another instance created inside the main application. Here is the solution to make your example work:

ClassA:


package
{
       import mx.controls.Alert;

       public class ClassA
       {
               public var classb:ClassB;
               public function ClassA()
               {
                       classb = new ClassB();
                       classb.addEventListener("CHECK",handleIt);

               }
              

               public function handleIt(event:MyEvent):void
               {
                       mx.controls.Alert.show(' Fingers crossed ');
               }

       }
}


Main Application:


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
       <![CDATA[

               public function init():void
               {
                       var classa:ClassA = new ClassA();
                       var classb:ClassB = new ClassB();
                       classa.classb.announceEvent();

               }

       ]]>
</mx:Script>

       <mx:Button id="btn" label="Check" click="init()"/>
</mx:Application>


Hope you understood where you went wrong.

Regards,
Venkat
www.venkatv.com

riaflexible

unread,
Nov 24, 2008, 11:50:43 PM11/24/08
to Flex India Community
Thanks Venkat, It's working now .

On Nov 24, 10:01 pm, "Venkat Viswanathan" <helloven...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages