PDA

View Full Version : dispatchEvent problem


MontyCoder
02-12-2009, 05:55 AM
Hi all
i am having problem with dispatchEvent, i want to pass String value while dispatchEvent.
here is the code

dispatchEvent(new CustomEventClass(CustomEventClass.MY_STRING,tempSt r));

listener code

private function init():void{ this.addEventListener(CustomEventClass.MY_STRING,g otEvent);
}
private function gotEvent(evt:CustomEventClass):void{
var myStr:String = evt.myString;
Alert.show("got it "+myStr);
}

custom event class

package assets
{
import flash.events.Event;

public class CustomEventClass extends Event
{
public static const MY_STRING:String = "myString";
public var myString:String;
public function CustomEventClass(type:String,myString:String=null)
{
super(type);
this.myString = myString;
}
override public function clone():Event{
return new CustomEventClass(type,myString);
}
}
}


when i use these codes in only page of file it works fine.

But i want to open another title window.. and i want to receive that String in that Title window.
Event is dispatching from main page.. but its not listing from titile window...
in Title window page i am using same code...

can any one tell me where i am wrong..?
thanks
:)

Sekhar
02-12-2009, 06:45 AM
Looks like dispatchEvent() is on the parent, but addEventListener() is on the TitleWindow. Change the dispatchEvent() to titleWindow.dispatchEvent() and you should be good (where "titleWindow" is the id of your TitleWindow, replace as necessary).

MontyCoder
02-12-2009, 07:12 AM
no its not working, getting error for null object reference

private var myTitle:TitleWindow;
private var myTW:myNewWindow;
private function openNewWindow():void{
myTitle.dispatchEvent(new CustomEventClass(CustomEventClass.MY_STRING,tempSt r));
myTitle = PopUpManager.createPopUp(this,myNewWindow,true)as TitleWindow;
myTitle.showCloseButton = true;
PopUpManager.centerPopUp(myTitle);
}

Beo
02-12-2009, 08:33 AM
hi,

In case you need to dispatch the custom event from title window to it's parent,
referencing this thread:
http://www.actionscript.org/forums/showthread.php3?t=152724&highlight=titlewindow+event

in case you want to get value from the parent to display on the tile window after open it:

in the parent:
private function openTilteWindow():void
{
var p:IFlexDisplayObject = PopUpManager.createPopUp(this, TitleWindowView, true);
p.addEventListener(Event.CLOSE, onTitleWindowClose);
TitleWindowView(p).childObjectParam= parentObjectParam;
PopUpManager.centerPopUp(p);
}

in the title window:

[Bindable]
private var _childObjectParam:oneObject= new oneObject();
public function set childObjectParam(childObjectParam:oneObject):void {
//set the value to _childObjectParam
}

Maybe it's useful

MontyCoder
02-12-2009, 08:46 AM
i don't need to dispatchEvent from Title Window.

What i am looking is:-
From main(stage) window popup window will open. From main window i want to pass a string to title window(popup window), via using custom dispatch Event.

Above mentioned code works fine, while using for same main(stage) window only. But when i open title window and want to pass string to title window. Its not working.?

Sekhar
02-12-2009, 02:59 PM
no its not working, getting error for null object reference

private var myTitle:TitleWindow;
private var myTW:myNewWindow;
private function openNewWindow():void{
myTitle.dispatchEvent(new CustomEventClass(CustomEventClass.MY_STRING,tempSt r));
myTitle = PopUpManager.createPopUp(this,myNewWindow,true)as TitleWindow;
myTitle.showCloseButton = true;
PopUpManager.centerPopUp(myTitle);
}

It will work, the null error is because you're accessing myTitle before you create it. Below is a quick working example.

----- MAIN -----
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">
<local:MyTitleWindow id="myTitleWindow"/>
<mx:Button x="410" label="Dispatch Event" click="myTitleWindow.dispatchEvent(new Event(MyTitleWindow.WHATEVER));"/>
</mx:Application>
----- MyTitleWindow -----
<mx:TitleWindow creationComplete="init();" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300">
<mx:Script>
<![CDATA[
public static var WHATEVER:String = "whatever";

private function init():void {
addEventListener(WHATEVER, function(event:Event):void {
messageField.text = event.toString();
});
}
]]>
</mx:Script>
<mx:Text id="messageField"/>
</mx:TitleWindow>

MontyCoder
02-16-2009, 11:44 AM
@Sekhar:

thanks a lot, got my mistake.. where i was wrong..
it worked..
sorry for late reply..... was little busy on work..
once again thanks
:)