AS3 从类调度事件

发布于 2024-08-09 23:40:25 字数 1804 浏览 5 评论 0原文

我有一个类发出 url 请求并存储该请求中的数据。当数据被检索、解析并存储到数组中时,我将发送一个调度事件,我在文档类中侦听该事件。在文档类的事件处理函数中,我正在访问从该类编译的数组。类内的数组有 15 个值,但是在我的事件处理函数中,我只从中检索一个值,这是数组中的最后一个值。我在下面发布了我的代码。我是否应该使用dispatchEvent 来检索所有数组值而不是一个数组值?

谢谢!

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

   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
  {
    //payload has 15 values
    return payload;
  }
 }
}

文档类

    public function queryServer(url:String):void
    {
        grabData = new DataGrab();
        grabData.addEventListener(Event.COMPLETE, dataReadyHandler);
        resultData  = new Array();
        grabData.init(url);

    }

    public function dataReadyHandler(e:Event):void
    {
        grabData = e.target as DataGrab;
        resultData  = grabData.getResults();

            //only has 1 value, the last value in the payload array
        trace(resultData);
    }

I have a class which makes a url request and stores data from that request. When the data is retrieved, parsed, and stored into an array, I'm sending a dispatch event which I listen for in my document class. In the event handler function in the document class, I'm accessing the array that was compiled from the class. The array inside the class has 15 values, however in my event handler function, I'm only retrieving one value from it, which is the last value in the array. I posted my code below. Is there a different way I'm supposed to use the dispatchEvent in order to retrieve all of the array values instead of one?

Thanks!

Class

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

   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
  {
    //payload has 15 values
    return payload;
  }
 }
}

Document Class

    public function queryServer(url:String):void
    {
        grabData = new DataGrab();
        grabData.addEventListener(Event.COMPLETE, dataReadyHandler);
        resultData  = new Array();
        grabData.init(url);

    }

    public function dataReadyHandler(e:Event):void
    {
        grabData = e.target as DataGrab;
        resultData  = grabData.getResults();

            //only has 1 value, the last value in the payload array
        trace(resultData);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耶耶耶 2024-08-16 23:40:25

你处理它的方式看起来不错。我有一种感觉,你的名字数组的长度只有 1。在 a 中记录一下,看看这是否是问题所在。

for (var key:Object in names) 
{ 
    trace("counter",counter);
    payload[counter] = [names[key].id, names[key].email]; 
    counter++; 
} 

the way you are handling it looks fine. I have a feeling that the length of your names array is only 1. Put a trace in a see if thats the problem.

for (var key:Object in names) 
{ 
    trace("counter",counter);
    payload[counter] = [names[key].id, names[key].email]; 
    counter++; 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文