Dispatch Event in Class


i have class remoting call. oncethe data returned, want dispatch event within class main component can result , update datagrid accordingly. can dispatch events no problem when dispatchevent not in class. unless extend eventdispatcher class, error on:
dispatchevent(new event("updateassignedemployeese", true, true));

i believe event not dispatching because parent component cannot listen it. there else need on parent component?

package components.objects
{
import flash.events.event;
import flash.events.eventdispatcher;

public class updateemployeetojob extends eventdispatcher
{
import mx.controls.alert;
import flash.events.mouseevent;
import flash.events.event;
import flash.events.eventdispatcher;
include "../../scripts/connection.as";
public var result:object = new object();

public function saverecord(myobj:object):void {
//showobjvalues(empjobobj);
trace ("ud " + usedatabase)
var passedvars : array = new array (myobj);
gateway = new remotingconnection(usedatabase);
gateway.call("jobs.addupdate", new responder(onresult, onfault), passedvars);
}

private function onresult(r:object):void {
//rebind data
trace ("employee added " + r.tostring());
result = r;
dispatchevent(new event("updateassignedemployeese", true, true));

}

private function onfault(f:string):void {
alert.show ("error updating employee job", "update error");
}
}
}

if create active valueobject class hold returned data. extend event dispatcher in valueobject class. populate class , send it's own events off , multiple controls respond event , ever need. use method when 1 of classes extends class not have eventdispatcher in it's inheritance chain.

i wrote friend example welcome it. not use data grid easy use same technique purpose.

note: put these files in same directory test or change class paths.

something note method can specify event name in file instantiates activevo knows respond event.

hope applies need. have nice day!

*******************main application************************
<?xml version="1.0" encoding="utf-8"?>
<mx:application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationcomplete="initapp();">
<mx:script>
<![cdata[

import flash.events.mouseevent;
import flash.events.event;
import mx.controls.alert;
import mx.managers.popupmanager;

private var activevo:activevo;
private var myeventname:string = "mycustomeventname";

private function initapp():void{
this.opener.addeventlistener(mouseevent.click,dopopup,false,0,true);
this.activevo = activevo.getactivevo();
this.activevo.eventname = myeventname;
this.activevo.addeventlistener(this.myeventname,showactivevomessage,false,0,true);
}

private function showactivevomessage(event:event):void{
alert.show("eventname: "+this.activevo.eventname+"\ntitle: "+this.activevo.title+"\nmessage: "+this.activevo.message);
}

private function dopopup(event:mouseevent):void{
var popupwindow:customtitlewindow=customtitlewindow(popupmanager.createpopup(this, customtitlewindow, true));
popupwindow.title = "my custom title window";
popupmanager.centerpopup(popupwindow);
}
]]>
</mx:script>
<mx:metadata>
[event(name=this.myeventname, type="flash.events.event")]
</mx:metadata>
<mx:button id="opener" label="open title window" />
</mx:application>
******************end main application****************************

*****************custom titlewindow******************************
<?xml version="1.0" encoding="utf-8"?>
<mx:titlewindow xmlns:mx=" http://www.adobe.com/2006/mxml"
layout="vertical"
width="400"
height="300"
creationcomplete="initapp();"
close="closethiswindow();"
showclosebutton="true">
<mx:script>
<![cdata[
import mx.managers.popupmanager;

private var activevo:activevo;

private function initapp():void{
this.activevo = activevo.getactivevo();
this.activevo.title = this.title.tostring();
this.activevo.message = "message text set customtitlewindow";
}

private function closethiswindow():void{
this.activevo.ping();
this.activevo = null;
popupmanager.removepopup(this);
}
]]>
</mx:script>
<mx:metadata>
[event(name=this.activevo.eventname, type="flash.events.event")]
</mx:metadata>
<mx:text id="mytext" text="hello filler text window not dull! :d" />
</mx:titlewindow>
**************************end custom titlewindow***********************

**************************active value object*****************************
package
{
import flash.events.event;
import flash.events.eventdispatcher;
import flash.events.ieventdispatcher;

public class activevo extends eventdispatcher
{
private var sttitle:string = "";
private var stmessage:string = "";
private var steventname:string = "";
private static var instance:activevo;
private static var allowinstance:boolean = false;

public function activevo(target:ieventdispatcher=null)
{
if(allowinstance){
super(target);
} else {
throw new error("error: use activevo.getactivevo() instead of new keyword");
}
}

public static function getactivevo():activevo{
if(instance == null){
allowinstance = true;
instance = new activevo();
allowinstance = false;
}
return instance;
}

public function set title(_header:string):void{
this.sttitle = _header;
}

public function title():string{
return this.sttitle;
}

public function set message(_message:string):void{
this.stmessage = _message;
}

public function message():string{
return this.stmessage;
}

public function set eventname(_eventname:string):void{
this.steventname = _eventname;
}

public function eventname():string{
return this.steventname;
}

public function ping():void{
var e:event = new event(this.eventname,true);
this.dispatchevent(e);
}

}
}
***********************end active valueobject**************************


More discussions in Flex (Read Only)


adobe

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support