AS3 从类调度事件
我有一个类发出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你处理它的方式看起来不错。我有一种感觉,你的名字数组的长度只有 1。在 a 中记录一下,看看这是否是问题所在。
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.