PDA

View Full Version : event handler problem


neurofaux
11-26-2008, 07:59 PM
folks,

i'm trying to return some xml data assigned to a local private variable via an event handler function, and then have that value returned via a getter but i seem to be losing the data upon exiting the event handler function:


loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("data.xml"));

private function onComplete(e:Event):void {
_data = loader.data; // or e.target.data, doesn't make a diff
}

// however...
public function get Data():XMLList {
return _data; // returns null
}


why am i losing the information stored in _data upon exiting onComplete(), if i'm storing it as a local private variable? this is what i want to do:


// object that contains code above
var xmlData:XMLData = new XMLData();
trace (xmlData.Data); // should return xml data


thanks,
n.

Sekhar
11-26-2008, 09:08 PM
Looks like you're creating XMLData and accessing XMLData.Data right away, before the class has a chance to load the XML.

neurofaux
11-27-2008, 07:48 PM
well, this is happening with simply assigning non-xml data to private variables in the event handler function onComplete, and then trying to return them.


this other person is having a similar problem. (http://www.actionscript.org/forums/showthread.php3?t=190283)

neurofaux
11-27-2008, 11:44 PM
anyone have any ideas? i did some further debugging and found out the order of execution is the issue:


public class DataXML {
public function DataXML() {
trace ("constructor");
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("test.xml"));
}
private function onComplete(e:Event):XMLList {
trace ("onComplete");
_data = loader.data; // or e.target.data, doesn't matter
}
public function get Data():XMLList {
trace ("Data property");
return _data;
}
}


then when instantiating DataXML, whether via Singleton method or not (results are the same):


public class DataTest {
private var dataXML:DataXML = new DataXML();
public function DataTest() {
trace (dataXML.Data); // returns null, naturally
}
}


even if i do Singleton, order of execution is the same:

public class DataTest {
public function DataTest() {
trace (DataXML.getInstance().Data); // returns null
}
}


the output results of the above code is the following:
constructor
Data property
null
onComplete

...but shouldn't the onComplete event handler execute before the Data property? why is the event handler, which is called in the DataXML constructor, called AFTER the Data getter instead of before? please help. is this a bug or am i missing something?


n.