因此,假设我们要加载一些XML -
var xmlURL:String = 'content.xml';
var xmlURLRequest:URLRequest = new URLRequest(xmlURL);
var xmlURLLoader:URLLoader = new URLLoader(xmlURLRequest);
xmlURLLoader.addEventListener(Event.COMPLETE, function(e:Event):void{
trace('loaded',xmlURL);
trace(XML(e.target.data));
});
如果我们需要知道该特定XML文档的源URL,我们就有这个变量来告诉我们,对吧?现在让我们假设xmlURL变量不是为了帮助我们 - 也许我们想要加载3个XML文档,按顺序命名,我们想在for循环中使用一次性变量:
for(var i:uint = 3; i > 0; i--){
var xmlURLLoader:URLLoader = new URLLoader(new URLRequest('content'+i+'.xml'));
xmlURLLoader.addEventListener(Event.COMPLETE, function(e:Event):void{
trace(e.target.src); // I wish this worked...
trace(XML(e.target.data));
});
}
突然间,这不是那么容易,对吧?
我讨厌你不能只说e.target.src或其他什么 - 有没有一种好方法将URLLoaders与他们加载数据的URL相关联?我错过了什么吗?这让我觉得不直观。
答案 0 :(得分:7)
for (var i:uint = 3; i > 0; i--) {
var src:URLRequest = new URLRequest('content'+i+'.xml');
var xmlURLLoader:URLLoader = new URLLoader(src);
xmlURLLoader.addEventListener(Event.COMPLETE, function(req:URLRequest):Function {
return function(e:Event):void {
trace(req); // Should work
// whatever you need to do
}
}(src));
}
你必须使用第二个函数来包装请求,否则所有三个事件监听器都将引用最后一个请求。
答案 1 :(得分:2)
sberry2A走在正确的轨道上。 Robert Penner最近向我们展示了本地URLStream周围的URLLoader is nothing but a tiny wrapper,因此滚动自己的URLLoader是一个合理的想法。
这是我滚动的方式:
public class URLLoader extends EventDispatcher{
private var _urlRequest:URLRequest; //the built-in URLLoader doesn't give you any access to the requested URL...
private var _stream:URLStream;
private var _dataFormat:String;// = "text"
private var _data:*;
private var _bytesLoaded:uint;// = 0
private var _bytesTotal:uint;// = 0
public function get request():URLRequest { return _urlRequest;}
public function get fileName():String { return _urlRequest.url.match(/(?:\\|\/)([^\\\/]*)$/)[1];}
public function get dataFormat():String { return _dataFormat;}
public function get data():* { return _data; }
public function get bytesLoaded():uint { return _bytesLoaded; }
public function get bytesTotal():uint { return _bytesTotal; }
public function URLLoader(request:URLRequest = null){
super();
_stream = new URLStream();
_stream.addEventListener(Event.OPEN, openHandler);
_stream.addEventListener(ProgressEvent.PROGRESS, progressHandler);
_stream.addEventListener(Event.COMPLETE, completeHandler);
_stream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
_stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
_stream.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
if (request != null){
load(request);
};
}
public function load(request:URLRequest):void {
_urlRequest = request;
_stream.load(_urlRequest);
}
public function close():void{
_stream.close();
}
private function progressHandler(event:ProgressEvent):void {
_bytesLoaded = event.bytesLoaded;
_bytesTotal = event.bytesTotal;
dispatchEvent(event);
}
private function completeHandler(event:Event):void{
var bytes:ByteArray = new ByteArray();
_stream.readBytes(bytes);
switch (_dataFormat){
case "binary":
_data = bytes;
break;
case "variables":
if (bytes.length > 0){
_data = new URLVariables(bytes.toString());
break;
};
case "text":
default:
_data = bytes.toString();
break;
};
trace("URLLoader: (" + fileName + "): " + event.type);
dispatchEvent(event);
}
private function openHandler(event:Event):void {
trace("URLLoader: ("+fileName+"): " + event.type +" "+_urlRequest.url);
dispatchEvent(event);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("URLLoader ("+fileName+"): " + event.type + " - " + event.text);
dispatchEvent(event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
dispatchEvent(event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("URLLoader ("+fileName+"): " + event.type + " - " + event.text);
dispatchEvent(event);
}
}
请注意request和fileName属性。要从COMPLETE事件处理程序获取URLRequest,您只需执行:var req:URLRequest = URLLoader(evtObj.target).request;