Search code examples
flashactionscripturlloader

AS3: How do I access the output of a URLLoader request outside of the onComplete function?


I'm trying to access the data_file value in the onComplete function, but to be honest I'm not even sure I'm calling it correctly.

public function version():String{
    var string_val = "";
    var data_file = "";

    //var request:URLRequest = new URLRequest("indexdynamic.php");
    var request:URLRequest = new URLRequest("http://localhost/indexdynamic.php");
        request.method = URLRequestMethod.POST;

    var verVars:URLVariables = new URLVariables();

    request.data = verVars

    var loader:URLLoader = new URLLoader(request);
    loader.addEventListener(Event.COMPLETE, onComplete);
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    loader.load(request);

    function onComplete(event:Event):String{
        string_val = event.target.data.Var.toString();

        if (string_val ==  "First Selection") {
            data_file = "fill_in_blanks_doi.xml";
        }else {
            data_file = "fill_in_blanks_2_doi.xml";
        }
        return data_file;
    }
    var data_file = onComplete(loader.COMPLETE);
    return data_file;
}

Solution

  • Typically Event Listener functions return void. So you don't get the value from an event listener through the return value.

    So step one:

    Remove: var data_file = onComplete(loader.COMPLETE); Remove: return data_file; // Remove Both lines. that return a value. Change the function header to return void:

    function onComplete(event:Event):void{
    

    Now you will have saved your "data_file" variable, and not overwritten it by accident. However, since this is an asynchronous operation, some part of your code is now waiting to be called now that you have the variable set. You need to add one more line inside onComplete() to continue execution of code in your app. It might be something like:

    processXML(data_file);
    

    or

    trace("Data File:", data_file);
    

    Hope that clears it up for you.

    === Edit ===

    I went back and read your code again, and I notice something else. You are declaring a function within a function. That's usually not such a great idea. I recommend moving your onComplete() function outside of the getVersion() function.

    Second - your getVersion() function returns a string, meaning it is a synchronous function - meainign the calling code expects an answer back instantly. This will not work. URLLoader is asynchronous - you call it, and at some indeterminitate point in the future it responds. So your code is free to do other stuff - like display a loader bar - while it gets a response form the server andthe data is transferred. When it is done, the laoder will call its listeners. Only at this point will your original goal of describing the version be possible.

    So - you will need to change your code to pause its execution until the load completes, then have the onComplete listener start the ball rolling again once the information is loaded and ready to be used.