In a previous post about the loader package, we explained how it can be used to bulk load different files into your application. That post also mentioned the possibility of adding custom mappings and load tasks to the loader for any use cases that are not supported by the default implementation. This post will explore these features in more detail.
Custom mapping
To recap, every time an URL is passed to the add method on a loader instance, a specialised loader task is created based on the file type. The file type is determined by file extension, and most common formats are mapped to constants in the DataTypes class inside the package. To see which formats are mapped to which data type, you can open LoaderManager, and view the registerBuiltInExtensionsToDataTypes method.
There are two ways of overriding this behaviour.
Firstly, you explicitly tell the loader what the data type associated with the file should be.
//create a new loader instance var loader:Loader = new Loader(); //add a file, and explicitly tell the loader it is a sound loader.add("SoundSample.wav", null, DataTypes.SOUND_TYPE); //start loading loader.execute();
If you are going to be loading many items of the same format however, it might be more convenient to map this format to a data type permanently.
//create a new loader instance var loader:Loader = new Loader(); //map the .wav extension to the sound data type LoaderManager.getInstance().registerExtensionToDataType("wav", DataTypes.SOUND_TYPE); //add a few files loader.add("SoundSample.wav"); loader.add("AnotherSoundSample.wav"); loader.add("YetAnotherSoundSample.wav"); //start loading loader.execute();
By calling the registerExtensionToDataType method on LoaderManager, a file format will be mapped to a data type, and the load task associated with this data type will be created each time you pass the URL of such a file to the add method. Because LoaderManager is a Singleton, this mapping will also work with every Loader instance you subsequently create.
Custom load tasks
As stated before, the Loader creates specialised load tasks for each data type mapped by LoaderManager. Because all these tasks extend the abstract LoadTask, it is possible to create your very own load tasks, and map them to data types, as outlined by the example below.
//create a new loader instance var loader:Loader = new Loader(); //maps a custom task to a data type LoaderManager.getInstance().registerTaskDefinitionToDataType(BetterSoundLoadTask, DataTypes.SOUND_TYPE); //add a sound file loader.add("SoundSample.wav"); //start loading loader.execute();
By calling the registerTaskDefiniftionToDataType method on LoaderManager, the task you pass as the first argument will be associated with the data type you pass as the second. As such, every time you pass a URL of a file with this extension to the add method on any loader, this task will be created to handle the loading.
Using the registerExtentionToDataType an registerTaskDefinitionToDataType methods in conjunction will enable you to entirely reconfigure the loader to match the needs of your application. Any conflicts with the default configuration are resolved internally by the LoaderManager, allowing you to expand the Loader without having to change any logic inside it.
To further facilitate extendibility, the LoadTask base class uses the Template Method design pattern. In the simplest implementation, a subclass only needs to override two protected methods (createLoader and addLoaderListeners) to enable custom loading. Since LoadTask itself extends Task, be mindful of calling the done method in your success handler, and the fail method in any error handlers you might create.
0 Responses to “Reconfiguring the Loader with custom load tasks”
Leave a Reply