ActionScript3:在一个 class 中通过 addEventListener 读取文件,如何将数据传递到另一个 class
ActionScript3: in one class read file by addEventListener, how to pass data to a different class
有很多问题与我的相似,但其中 none 解决了我的问题。
我有这个class -
package com.test
{
import flash.events.*;
import flash.net.*;
import com.test.LogUtils;
import mx.logging.ILogger;
public class LoadExtUrl extends EventDispatcher
{
private var baseUrl:String;
private var log:ILogger = LogUtils.getLogger(LoadExtUrl);
public function LoadExtUrl()
{
log.debug ("100 In LoadExtUrl()");
super(null);
}
public function loadBaseUrl():String
{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, urlLoader_completeHandler);
function urlLoader_completeHandler(event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
this.baseUrl = loader.data.baseurl;
dispatchEvent(new Event("GOTRESULTS"));
log.debug ("200 In LoadExtUrl, baseUrl="+this.baseUrl);
}
loader.load(new URLRequest("sri-config-files/url.properties"));
log.debug ("300 In LoadExtUrl, baseUrl="+this.baseUrl);
return this.baseUrl;
}
}
}
现在我想在许多其他 classes 中读取 baseUrl 的值。
在另一个class中我有以下代码-
public class UrlHelper
{
public static var myLoadExtUrl:LoadExtUrl = new LoadExtUrl();
public static var baseUrl:String;
public function UrlHelper()
{}
public static function getBaseUrl():void
{
myLoadExtUrl.addEventListener("GOTRESULTS", xmlLoadCompleted);
log.debug("400 In UrlHelper, baseUrl ="+baseUrl);
}
private static function xmlLoadCompleted(e:Event):void
{
baseUrl=myLoadExtUrl.loadBaseUrl();
log.debug("500 In UrlHelper, baseUrl ="+baseUrl);
}
}
记录序列 -
100 In LoadExtUrl()
300 In LoadExtUrl, baseUrl=null
200 In LoadExtUrl, baseUrl=http://abcxyz.com:8080/
400 In UrlHelper, baseUrl =null --> here only I need the value
我该如何解决这个问题?
我想我需要写下一些解释。
什么是异步操作?这是一个
的操作
- 需要一些(最初未知或不确定的)时间才能完成
- 不阻塞代码执行,简单的说,当你开始加载代码时,不会停止等待操作完成,它会立即开始执行其余代码,而不管该操作状态
因此,您正在构建的事物中的实际事件流是:
- UH class 告诉 LEU class 开始加载
- ...一段时间过去了...
- LEU class 检测到加载过程结束。
- 加载的数据可用。
- LEU 调度自定义事件。
- UH检测事件,最终CAN获取数据
因此,LoadExtUrl class:
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
public class LoadExtUrl extends EventDispatcher
{
private var baseUrl:String;
private var loader:URLLoader;
// Interface method.
public function loadBaseUrl():String
{
var aRequest:URLRequest;
// Form the HTTP request.
aRequest = new URLRequest;
aRequest.url = "sri-config-files/url.properties";
// Initiate the loading process.
loader = new URLLoader;
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, onLoad);
loader.load(aRequest);
// Data are NOT available yet at this point.
}
// Data loading COMPLETE handler.
private function onLoad(e:Event):void
{
// Data are AVAILABLE at this point.
// Extract the data.
baseUrl = loader.data.baseurl;
// Clean up.
loader.removeEventListener(Event.COMPLETE, onLoad);
loader = null;
// Tell anyone willing to listen about the data availability.
var anEvent:Event;
// Feel free to use predefined constants instead of custom
// event names. It will protect you against typo errors.
anEvent = new Event(Event.COMPLETE);
dispatchEvent(anEvent);
}
}
}
使用方法:
public class UrlHelper
{
static public var baseUrl:String;
static private var loadExt:LoadExtUrl;
// Interface method.
static public function getBaseUrl():void
{
// Data are NOT available yet at this point.
loadExt = new LoadExtUrl;
// Data are NOT available yet at this point.
// Subscribe to the very same event name
// that class is going to dispatch.
loadExt.addEventListener(Event.COMPLETE, onAnswer);
// Data are NOT available yet at this point EITHER.
// Loading is an asynchronous operation. We started
// the loading but we have to wait until the data are available.
}
// This handler will be invoked when data are available.
static private function onAnswer(e:Event):void
{
// Data are AVAILABLE at this point.
// Extract the data.
baseUrl = loadExt.baseUrl;
// Clean up.
loadExt.removeEventListener(Event.COMPLETE, onAnswer);
loadExt = null;
// You are free to use the obtained data at this point.
// ...
}
}
有很多问题与我的相似,但其中 none 解决了我的问题。
我有这个class -
package com.test
{
import flash.events.*;
import flash.net.*;
import com.test.LogUtils;
import mx.logging.ILogger;
public class LoadExtUrl extends EventDispatcher
{
private var baseUrl:String;
private var log:ILogger = LogUtils.getLogger(LoadExtUrl);
public function LoadExtUrl()
{
log.debug ("100 In LoadExtUrl()");
super(null);
}
public function loadBaseUrl():String
{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, urlLoader_completeHandler);
function urlLoader_completeHandler(event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
this.baseUrl = loader.data.baseurl;
dispatchEvent(new Event("GOTRESULTS"));
log.debug ("200 In LoadExtUrl, baseUrl="+this.baseUrl);
}
loader.load(new URLRequest("sri-config-files/url.properties"));
log.debug ("300 In LoadExtUrl, baseUrl="+this.baseUrl);
return this.baseUrl;
}
}
}
现在我想在许多其他 classes 中读取 baseUrl 的值。
在另一个class中我有以下代码-
public class UrlHelper
{
public static var myLoadExtUrl:LoadExtUrl = new LoadExtUrl();
public static var baseUrl:String;
public function UrlHelper()
{}
public static function getBaseUrl():void
{
myLoadExtUrl.addEventListener("GOTRESULTS", xmlLoadCompleted);
log.debug("400 In UrlHelper, baseUrl ="+baseUrl);
}
private static function xmlLoadCompleted(e:Event):void
{
baseUrl=myLoadExtUrl.loadBaseUrl();
log.debug("500 In UrlHelper, baseUrl ="+baseUrl);
}
}
记录序列 -
100 In LoadExtUrl()
300 In LoadExtUrl, baseUrl=null
200 In LoadExtUrl, baseUrl=http://abcxyz.com:8080/
400 In UrlHelper, baseUrl =null --> here only I need the value
我该如何解决这个问题?
我想我需要写下一些解释。
什么是异步操作?这是一个
的操作- 需要一些(最初未知或不确定的)时间才能完成
- 不阻塞代码执行,简单的说,当你开始加载代码时,不会停止等待操作完成,它会立即开始执行其余代码,而不管该操作状态
因此,您正在构建的事物中的实际事件流是:
- UH class 告诉 LEU class 开始加载
- ...一段时间过去了...
- LEU class 检测到加载过程结束。
- 加载的数据可用。
- LEU 调度自定义事件。
- UH检测事件,最终CAN获取数据
因此,LoadExtUrl class:
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
public class LoadExtUrl extends EventDispatcher
{
private var baseUrl:String;
private var loader:URLLoader;
// Interface method.
public function loadBaseUrl():String
{
var aRequest:URLRequest;
// Form the HTTP request.
aRequest = new URLRequest;
aRequest.url = "sri-config-files/url.properties";
// Initiate the loading process.
loader = new URLLoader;
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, onLoad);
loader.load(aRequest);
// Data are NOT available yet at this point.
}
// Data loading COMPLETE handler.
private function onLoad(e:Event):void
{
// Data are AVAILABLE at this point.
// Extract the data.
baseUrl = loader.data.baseurl;
// Clean up.
loader.removeEventListener(Event.COMPLETE, onLoad);
loader = null;
// Tell anyone willing to listen about the data availability.
var anEvent:Event;
// Feel free to use predefined constants instead of custom
// event names. It will protect you against typo errors.
anEvent = new Event(Event.COMPLETE);
dispatchEvent(anEvent);
}
}
}
使用方法:
public class UrlHelper
{
static public var baseUrl:String;
static private var loadExt:LoadExtUrl;
// Interface method.
static public function getBaseUrl():void
{
// Data are NOT available yet at this point.
loadExt = new LoadExtUrl;
// Data are NOT available yet at this point.
// Subscribe to the very same event name
// that class is going to dispatch.
loadExt.addEventListener(Event.COMPLETE, onAnswer);
// Data are NOT available yet at this point EITHER.
// Loading is an asynchronous operation. We started
// the loading but we have to wait until the data are available.
}
// This handler will be invoked when data are available.
static private function onAnswer(e:Event):void
{
// Data are AVAILABLE at this point.
// Extract the data.
baseUrl = loadExt.baseUrl;
// Clean up.
loadExt.removeEventListener(Event.COMPLETE, onAnswer);
loadExt = null;
// You are free to use the obtained data at this point.
// ...
}
}