0

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;
}

3 Answers3

1

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.

Plastic Sturgeon
  • 12,167
  • 4
  • 30
  • 46
0

You have to provide access to the data by writing it in a variable which is declared outside of the method.

Florian Salihovic
  • 3,760
  • 2
  • 15
  • 23
  • I've tried to create an instance variable called data_file and modify it in onComplete() with "this.data_file = ..." and its not working. I can set the data_file variable within version(), but I guess that "this"'s scope does not reach that 2nd level of nesting. Any suggestions? – Phil Apollo Aug 02 '12 at 17:11
  • Yes it does, but it seems like you want to get on synchronously. From within the callback, better call method to move on with the code. Also, i suggest avoiding deep method nesting. This is more ActionScript 2 style, not idiomatic ActionScript 3. – Florian Salihovic Aug 02 '12 at 18:21
0

You could always store the output into a global array, which would be accessible from anywhere in the app.

var myArray:Array = new Array();

function onComplete(e:Event):void {
   myArray.push(e.target.data);
}
shaneparsons
  • 1,133
  • 1
  • 14
  • 23