/** * VERSION: 1.84 * DATE: 2011-03-23 * AS3 * UPDATES AND DOCS AT: http://www.greensock.com/loadermax/ **/ package com.greensock.loading { import com.greensock.loading.core.LoaderItem; import flash.events.Event; import flash.events.ProgressEvent; import flash.net.URLLoader; /** Dispatched when the loader's httpStatus value changes. **/ [Event(name="httpStatus", type="com.greensock.events.LoaderEvent")] /** Dispatched when the loader experiences a SECURITY_ERROR while loading or auditing its size. **/ [Event(name="securityError", type="com.greensock.events.LoaderEvent")] /** * Loads generic data which can be text (the default), binary data, or URL-encoded variables.

* * If the format vars property is "text", the content will be a String containing the text of the loaded file.

* If the format vars property is "binary", the content will be a ByteArray object containing the raw binary data. (See also: BinaryDataLoader)

* If the format vars property is "variables", the content will be a URLVariables object containing the URL-encoded variables

* * OPTIONAL VARS PROPERTIES
* The following special properties can be passed into the DataLoader constructor via its vars * parameter which can be either a generic object or a DataLoaderVars object:
*
* * Note: Using a DataLoaderVars instance * instead of a generic object to define your vars is a bit more verbose but provides * code hinting and improved debugging because it enforces strict data typing. Use whichever one you prefer.

* * @example Example AS3 code: import com.greensock.loading.~~; import com.greensock.events.LoaderEvent; import flash.utils.ByteArray; import flash.net.URLVariables; //create a DataLoader for loading text (the default format) var loader:DataLoader = new DataLoader("assets/data.txt", {name:"myText", requireWithRoot:this.root, estimatedBytes:900}); //start loading loader.load(); //Or you could put the DataLoader into a LoaderMax. Create one first... var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler}); //append the DataLoader and several other loaders queue.append( loader ); queue.append( new DataLoader("assets/variables.txt", {name:"myVariables", format:"variables"}) ); queue.append( new DataLoader("assets/image1.png", {name:"myBinary", format:"binary", estimatedBytes:3500}) ); //start loading the LoaderMax queue queue.load(); function progressHandler(event:LoaderEvent):void { trace("progress: " + event.target.progress); } function completeHandler(event:LoaderEvent):void { var text:String = LoaderMax.getContent("myText"); var variables:URLVariables = LoaderMax.getContent("myVariables"); var binary:ByteArray = LoaderMax.getContent("myBinary"); trace("complete. myText: " + text + ", myVariables.var1: " + variables.var1 + ", myBinary.length: " + binary.length); } function errorHandler(event:LoaderEvent):void { trace("error occured with " + event.target + ": " + event.text); } * * Copyright 2011, GreenSock. All rights reserved. This work is subject to the terms in http://www.greensock.com/terms_of_use.html or for corporate Club GreenSock members, the software agreement that was issued with the corporate membership. * * @see com.greensock.loading.data.DataLoaderVars * * @author Jack Doyle, jack@greensock.com */ public class DataLoader extends LoaderItem { /** @private **/ private static var _classActivated:Boolean = _activateClass("DataLoader", DataLoader, "txt,js"); /** @private **/ protected var _loader:URLLoader; /** * Constructor * * @param urlOrRequest The url (String) or URLRequest from which the loader should get its content. * @param vars An object containing optional configuration details. For example: new DataLoader("text/data.txt", {name:"data", onComplete:completeHandler, onProgress:progressHandler}).

* * The following special properties can be passed into the constructor via the vars parameter * which can be either a generic object or a DataLoaderVars object:
* * @see com.greensock.loading.data.DataLoaderVars */ public function DataLoader(urlOrRequest:*, vars:Object=null) { super(urlOrRequest, vars); _type = "DataLoader"; _loader = new URLLoader(null); if ("format" in this.vars) { _loader.dataFormat = String(this.vars.format); } _loader.addEventListener(ProgressEvent.PROGRESS, _progressHandler, false, 0, true); _loader.addEventListener(Event.COMPLETE, _receiveDataHandler, false, 0, true); _loader.addEventListener("ioError", _failHandler, false, 0, true); _loader.addEventListener("securityError", _failHandler, false, 0, true); _loader.addEventListener("httpStatus", _httpStatusHandler, false, 0, true); } /** @private **/ override protected function _load():void { _prepRequest(); _loader.load(_request); } /** @private scrubLevel: 0 = cancel, 1 = unload, 2 = dispose, 3 = flush **/ override protected function _dump(scrubLevel:int=0, newStatus:int=0, suppressEvents:Boolean=false):void { if (_status == LoaderStatus.LOADING) { try { _loader.close(); } catch (error:Error) { } } super._dump(scrubLevel, newStatus, suppressEvents); } //---- EVENT HANDLERS ------------------------------------------------------------------------------------ /** @private Don't use _completeHandler so that subclasses can set _content differently and still call super._completeHandler() (otherwise setting _content in the _completeHandler would always override the _content previously set in sublcasses). **/ protected function _receiveDataHandler(event:Event):void { _content = _loader.data; super._completeHandler(event); } //---- GETTERS / SETTERS ------------------------------------------------------------------------- } }