Jump to content

ActionScript 3, setting class properties in addEventListener


skaterdav85
 Share

Recommended Posts

I have 2 methods and 2 properties in an Actionscript 3 class. The first method fetchXMLData() gets data from an XML file. In the addEventListener method on the urlLoader object, I pass it a listener which tries to set the returned data to the xmlData class property. However, it never sets it, and I know the data comes back because I can alert out urlLoader.data using the mx.controls.Alert component. Anyone have an idea of what is going on?

public var urlLoader:URLLoader;public var xmlData:String; public function getXMLData():String {			return this.xmlData;}	  public function fetchXMLData():void {		  			urlLoader = new URLLoader();			urlLoader.addEventListener(Event.COMPLETE, function(e:Event):void {				this.xmlData = urlLoader.data;			});		  			urlLoader.load(new URLRequest("tours.xml"));			   }

Link to comment
Share on other sites

So I found out that using anonymous functions in AS can be kind of weird and people recommend not using them in your Actionscript. Instead of using an anonymous function, when the Complete event occurs, I call a class method and set the property. It's also worth noting that I need to dispatch a custom event so that I can listen for the custom event and call the getter method when the response is completed. When you dispatch a custom event, don't use an anonymous function like what I am doing here. It ends up getting dispatched to a different context and you can't listen for the event on your object. Im not sure I like Actionscript 3. Apparently it is based on EcmaScript 4 which many JS developers disliked. Maybe it just takes getting used to, but overall the Flex framework and Actionscript 3 are kind of neat.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...