Is there a more concise way to use a URLLoader?
I'm want to use a URLLoader in one line, but I know Internet Explorer has given me problems loading local files when the listener is added after the load
call.
new URLLoader(new URLRequest('http://example.com')).addEventListener(
Event.COMPLETE, handleLoadedData
);
I know the verbose, but safe way of using a URLLoader is:
var request:URLRequest = new URLRequest('http://example.com');
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, handleLoadedData);
loader.load(re开发者_StackOverflow中文版quest);
What's the most concise way of using URLLoader that works with local files?
What about creating a class that offers a simple way to load data and just reuse it?
I mean something simple like this:
package yourdomain.net
{
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class DataLoader {
public var url:String;
public var completeCallback:Function;
private var loader:URLLoader;
public function DataLoader(url:String, completeCallback:Function) {
this.url = url;
this.completeCallback = completeCallback;
}
public function load():void {
var request:URLRequest = new URLRequest(url);
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, this.completeCallback);
loader.load(request);
}
public static function load(url:String, completeCallback:Function):void {
var dataLoader:DataLoader = new DataLoader(url, completeCallback);
dataLoader.load();
}
}
}
And then, you'd use it in one line like this:
DataLoader.load('http://example.com',handleDataComplete);
I've proposed a similar idea here. It's basically the same idea, just adding some basic error handling.
DO some code :) and rest then make a Static method in AS3 class like that
public final class MyLoaders{
static public function dataLoader(var url:String, listener:Function):URLLoader{
// Code Here
}
}
you can call loader as anywhere in APP
MyLoaders.dataLoader( url, listener);
Hopes that helps
enjoy :)
精彩评论