Packagecom.threerings.util
Classpublic class MultiLoader

Easy loader for many things, including managing multiple downloads. More documentation coming.



Protected Properties
 PropertyDefined by
  _complete : Function
MultiLoader
  _forEach : Boolean
MultiLoader
  _remaining : int = 0
MultiLoader
  _result : Object
MultiLoader
  _targetsToKeys : Dictionary
MultiLoader
Public Methods
 MethodDefined by
  
MultiLoader(sources:Object, completeCallback:Function, generatorFn:Function = null, forEach:Boolean = false, isCompleteCheckFn:String = null, errorTypes:Array = null, completeType:String = null)
Coordinate loading some asynchronous objects.
MultiLoader
  
getContents(sources:Object, completeCallback:Function, forEach:Boolean = false, appDom:ApplicationDomain = null):void
[static] Load one or more sources and return DisplayObjects.
MultiLoader
  
getLoaders(sources:Object, completeCallback:Function, forEach:Boolean = false, appDom:ApplicationDomain = null):void
[static] Exactly like getContents() only it returns the Loader objects rather than their contents.
MultiLoader
  
loadClasses(sources:Object, appDom:ApplicationDomain, completeCallback:Function):void
[static] Loads classes into the specified ApplicationDomain.
MultiLoader
Protected Methods
 MethodDefined by
  
checkReport(key:*):void
MultiLoader
  
handleComplete(event:Event):void
MultiLoader
  
handleError(event:ErrorEvent):void
MultiLoader
  
processProperty(retval:Object, testClass:Class, prop:String):Object
[static] Utility method used in this class.
MultiLoader
Protected Constants
 ConstantDefined by
  _activeMultiLoaders : Dictionary
[static]
MultiLoader
Property detail
_completeproperty
protected var _complete:Function
_forEachproperty 
protected var _forEach:Boolean
_remainingproperty 
protected var _remaining:int = 0
_resultproperty 
protected var _result:Object
_targetsToKeysproperty 
protected var _targetsToKeys:Dictionary
Constructor detail
MultiLoader()constructor
public function MultiLoader(sources:Object, completeCallback:Function, generatorFn:Function = null, forEach:Boolean = false, isCompleteCheckFn:String = null, errorTypes:Array = null, completeType:String = null)

Coordinate loading some asynchronous objects.

Parameters
sources:Object — An Array, Dictionary, or Object of sources, or just a single source.
 
completeCallback:Function — the function to call when complete.
 
generatorFn:Function (default = null) — a function to call to generate the IEventDispatchers, or null if the source values are already ready to go.
 
forEach:Boolean (default = false) — whether to call the completeCallback for each source, or all-at-once at the end. If forEach is used, keys will never be returned.
 
isCompleteCheckFn:String (default = null) — a function to attempt to call on the dispatcher to see if it's already complete after generation.
 
errorTypes:Array (default = null) — an Array of event types that will be dispatched by the loader. If unspecifed, all the normal error event types are used.
 
completeType:String (default = null) — the event complete type. If unspecifed
Method detail
checkReport()method
protected function checkReport(key:*):voidParameters
key:*
getContents()method 
public static function getContents(sources:Object, completeCallback:Function, forEach:Boolean = false, appDom:ApplicationDomain = null):void

Load one or more sources and return DisplayObjects.

Parameters
sources:Object — an Array, Dictionary, or Object containing sources as values, or a single source value. The sources may be Strings (representing urls), URLRequests, ByteArrays, or a Class that can be instantiated to become a URLRequest or ByteArray. Note that the format of your sources Object dictates the format of the return Object.
 
completeCallback:Function — the function to call when complete. The signature should be: function (value :Object) :void. Note that the structure of the return Object is dictated by the sources parameter. If you pass in an Array, you get your results in an Array. If you use a Dictionary or Object, the results will be returned as the same, with the same keys used in sources now pointing to the results. If your sources parameter was just a single source (like a String) then the result will just be a single result, like a DisplayObject. Each result will be a DisplayObject or an Error describing the problem.
 
forEach:Boolean (default = false) — if true, each value or error will be returned as soon as possible. The values or errors will be returned directly to the completeCallback. Any keys are lost, so you probably only want to use this with an Array sources.
 
appDom:ApplicationDomain (default = null) — the ApplicationDomain in which to load the contents, or null to specify that it should load in a child of the current ApplicationDomain.

Example
Load one embed, add it as a child.
     MultiLoader.getContents(EMBED_CONSTANT, addChild);
     

Load 3 embeds, add them as children.
     MultiLoader.getContents([EMBED1, EMBED2, EMBED3], addChild, true);
     

Load multiple URLs, have the contents returned to the result function one at a time.
     function handleComplete (result :Object) :void {
        // process a result here. Result may be a DisplayObject or an Error.
     };
     
     var obj :Object = {
         key1: "http://somehost.com/someImage.gif",
         key2: "http://somehost.com/someOtherImage.gif"
     };
     
     MultiLoader.getContents(obj, handleComplete, true);
     

Load 3 embeds, wait to handle them until they're all loaded.
     function handleComplete (results :Array) :void {
         // process results here
     };
          MultiLoader.getContents([EMBED1, EMBED2, EMBED3], handleComplete);
     

getLoaders()method 
public static function getLoaders(sources:Object, completeCallback:Function, forEach:Boolean = false, appDom:ApplicationDomain = null):void

Exactly like getContents() only it returns the Loader objects rather than their contents.

Parameters
sources:Object
 
completeCallback:Function
 
forEach:Boolean (default = false)
 
appDom:ApplicationDomain (default = null)

See also

getContents()

Example
Advanced usage: Loading classes.
     // A holder for new classes, created as a child of the system domain.
     var appDom :ApplicationDomain = new ApplicationDomain(null);
     
function handleComplete (results :Object) :void { // now we can retrieve classes var clazz :Class = appDom.getDefinition("com.package.SomeClass") as Class; }
// load all the classes contained in the specified sources MultiLoader.getLoaders([EMBED, "http://site.com/pack.swf"], handleComplete, false, appDom);
[Embed(source="resource.swf", mimeType="application/octet-stream")] private static const EMBED :Class;

handleComplete()method 
protected function handleComplete(event:Event):voidParameters
event:Event
handleError()method 
protected function handleError(event:ErrorEvent):voidParameters
event:ErrorEvent
loadClasses()method 
public static function loadClasses(sources:Object, appDom:ApplicationDomain, completeCallback:Function):void

Loads classes into the specified ApplicationDomain. The complete callback receives either the same ApplicationDomain or no arguments.

Parameters
sources:Object
 
appDom:ApplicationDomain
 
completeCallback:Function
processProperty()method 
protected static function processProperty(retval:Object, testClass:Class, prop:String):Object

Utility method used in this class.

Parameters
retval:Object
 
testClass:Class
 
prop:String

Returns
Object
Constant detail
_activeMultiLoadersconstant
protected static const _activeMultiLoaders:Dictionary