Delayed function return? :/

I know nesting functions inside functions is bad bad, so if I want a function to return a string that is fetched from a php file … how would that really look?

I mean


private static function getInfo():String {
  var urlVariables:URLVariables = new URLVariables();
  var urlRequest:URLRequest = new URLRequest("moo.php"); 
  var urlLoader:URLLoader = new URLLoader();
  urlLoader.addEventListener(Event.COMPLETE, phpDone);
  urlLoader.load(urlRequest);
  return .... ? :/
}
private static function phpDone(pEvent:Event):String {
  var xml:XML = new XML(urlLoader.data);
 return xml.userdata.text()
}

Thanks for any insight.

You can’t return from events. You have to wait until the event occurs and then run additional code at that time.

I shed a tear.

Thanks senocular.

[U]This is the class[/U] I wrote to deal with XML for me. The usage is something like this:


var myNewXML:CoreXML = new CoreXML("path.xml");
myNewXML.addEventListener(Event.COMPLETE, myFunction);

function myFunction(e:Event):void
{
     trace(myNewXML.xmlData);
}

This is the full code:


package com.rragona.utils.coreXML
{
        
        import flash.events.Event;
        import flash.events.IOErrorEvent;
        import flash.events.SecurityErrorEvent;
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        
        
        /**
        * ...
        * @author rragona
        * @version 1.0
        * 
        * This is a generic XML class that can be extended for more specific results.
        * 
        */
        
        public class CoreXML extends URLLoader
        {
                private var _xmlLoader;
                private var _xmlRequest;
                private var _xmlData;
                
                /**
                 * @usage       This class accepts a single parameter - the path that you wish to load. 
                 * Example usage: var myVar:CoreXML = new CoreXML("myPath.xml");
                 * 
                 * @param       path    Path to the XML document that you wish to load
                 */ 
                
                public function CoreXML(path:String) 
                {
                        _xmlLoader = new URLLoader();
                        _xmlRequest = new URLRequest(path);
                        
                        initListeners();
                        
                        _xmlLoader.load(_xmlRequest);
                }
                
                public function get xmlData():XML
                {
                        return _xmlData;
                }
                
                /* Listeners and Events */
                
                private function initListeners():void
                {
                        _xmlLoader.addEventListener(Event.COMPLETE, postXMLLoad);
                        _xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, catchIOError);
                        _xmlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, catchSecurityViolation);
                }
                
                private function postXMLLoad(e:Event):void
                {
                        _xmlData = new XML(e.target.data);
                        dispatchEvent(new Event(Event.COMPLETE));
                }
                
                private function catchIOError(event:IOErrorEvent):void
                {
                        trace("CoreXML Error Caught: "+event);
                }
                
                private function catchSecurityViolation(e:SecurityErrorEvent):void
                {
                        trace("CoreXML Security Violation Caught: " + e);
                }
                
        }
        
}

This allows you to have an independent XML object for each feed that you need. I find it especially useful when handling a lot of feeds at once.

Oops, I don’t want to edit that post because it’ll screw up all the AS formatting, but what I wanted you to see is a good method for handling chunks of data while allowing for load times. The private vars protect encapsulation. If you wanted, you could make sure that the getter only returned the XML object after the XML load was complete - but really you should just make sure not to try to access the data until after the COMPLETE event has been fired.