Actionscript 3: returning array in a class

actionscript-3arraysflash

I built a class that parses JSON data, stores it in an array, and now I want to return that data so that it can be stores in an array in my root AS file. I'm eventually trying to pass the returned array to another class. My class looks like this:

package com.src
{
 import flash.display.Sprite;
 import flash.net.URLRequest;
 import flash.net.URLLoader;
 import flash.events.*;
 import com.src.serialization.json.JSON;

 public class DataGrab extends Sprite
 {
  public var jsonData:Object;


  public function DataGrab()
  {

  }

  public function init(resource:String):void
  {
   var loader:URLLoader = new URLLoader();
   var request:URLRequest = new URLRequest(resource);
   loader.addEventListener(Event.COMPLETE, onComplete);
   loader.load(request);
  }

  private function onComplete(e:Event):void
  {
   var loader:URLLoader = URLLoader(e.target);
   jsonData = JSON.decode(loader.data);
     }

  public function getResults():Array
  {
   var people = jsonData.people;
   var names:Array = people.name;
   var payload  = new Array();
   var counter:Number = 0;
   for (var key:Object in names)
   {

    payload[counter] = [names[key].id, names[key].email];
    counter++;
   }
   return payload;
  }
 }
}

When I call the class in the root AS file, my code looks like:

var grabData:DataGrab = new DataGrab();
grabData.init(url:String);
var resultData:Array  = grabData.payload();

I keep getting the following error however:

1061: Call to a possibly undefined method payload through a reference with static type com.src.DataGrab.

Does anyone have advice on what might be wrong with my class, or a more logical way to write the getResults() function so that I can get retrieve the array being generated by this class?

Thanks.

Best Solution

Problem is you are trying to write your code synchronously but a network action is asynchronous. That is to say, you have to wait around for the network action to complete before requesting the data. Your best bet, as TandemAdam said is to use an event listener. Something like this:

package com.src
{
 import flash.display.Sprite;
 import flash.net.URLRequest;
 import flash.net.URLLoader;
 import flash.events.*;
 import com.src.serialization.json.JSON;

 public class DataGrab extends Sprite
 {
  public var payload:Array;

  public function DataGrab()
  {
  }

  public function init(resource:String):void
  {
   var loader:URLLoader = new URLLoader();
   var request:URLRequest = new URLRequest(resource);
   loader.addEventListener(Event.COMPLETE, onComplete);
   loader.load(request);
  }

  private function onComplete(e:Event):void
  {
   var loader:URLLoader = URLLoader(e.target);
   var jsonData:Object = JSON.decode(loader.data);
   // Format the data
   var people = jsonData.people;
   var names:Array = people.name;
   var counter:Number = 0;
   payload = new Array();
   for (var key:Object in names)
   {
    payload[counter] = [names[key].id, names[key].email];
    counter++;
   }
   dispatchEvent(new Event(Event.COMPLETE));
  }

  public function getResults():Array
  {
    return payload;
  }
 }
}

Then you can use it like this:

var grabData:DataGrab = new DataGrab();
function dataReadyHandler(event:Event):void {
    var grabData:DataGrab = event.target as DataGrab;
    trace(grabData.getResults());
    // or
    trace(grabData.payload);
}
grabData.addEventListener(Event.COMPLETE, dataReadyHandler);
grabData.init(url:String);

I strongly suggest reading more about the EventDispatcher class and asynchronous programming. This may help also http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000838.html