DataLib – New release of the loader class

DataLib is a loader class that manages all load requests of your application and stores all loaded data for future requests. I´ve been using it previous version for about an year and half and it’s been working just fine, but I’ve decided to release a new version with more features and better usage.

The concept is simple. Every instance of DataLib have a load queue, and every instance is placed on a static queue that manages all requests. By default, 3 itens can be loaded at the same time, but you can change the number of slots as your wish. This way, one single giant file doesn’t hold your application until it is loaded, you can use other slots to load all other files that your application needs.

Another advantage is that you can load files from a dynamic url, i.e. a php that returns a xml or an image depending on what parameters you passes to it.

You can also get any loaded data from any place of your application even if you don’t have acess to the instance that requested that file. This is because every file is stored at the static class. Another thing is that if you call a file that has already been loaded, it takes the data from the DataLib library, saving some (or much) loading time. If you care about memory usage, you can delete data from the library whenever you want.

The usage is simple. Create an instance of the DataLib and add how many itens you want to the queue. You need to pass a path or URLRequest and a type of file. Optionally you can pass an object that will be returned when the item is complete. For images and swf you can pass a loaderContext, if you don’t, the DataLib default loaderContext (checkPolicyFile false, current application domain and no security domain) will be applied. Every time you add an item to the DataLib queue it return an DataLibItem instance that extends an EventDispatcher, so you can add event listeners to the entire queue or to every single item.

import com.andreanaya.net.DataLib;
 
var dataLib:DataLib = new DataLib();
 
dataLib.addItem("image.jpg", DataLib.TYPE_IMAGE).addEventListener(DataLibEvent.PROGRESS, onItemProgress);
 
var request:URLRequest();
request.url = "mylist.php";
 
var variables:URLVariables = new URLVariables();
variables.product = "car";
variables.color = "red";
 
request.data = variables;
 
request.method = URLRequestMethod.POST;
 
var item:DataLibItem = dataLib.addItem(request, DataLib.TYPE_TEXT, {totalCars:2});
item.addEventListener(DataLibEvent.COMPLETE, onItemComplete);
 
dataLib.addEventListener(DataLibEvent.COMPLETE, onComplete);
 
dataLib.load();
 
function onItemProgress(e:DataLibEvent):void
{
	trace("loading", e.id, Math.floor(e.bytesLoaded*100/e.bytesTotal)+"%");
}
 
function onItemComplete(e:DataLibEvent):void
{
	var xml:XML = new XML(e.data);
	trace(xml, e.returnObject.totalCars);
}
 
function onComplete(e:DataLibEvent):void
{
	var image:Bitmap = DataLib.getData("image.jpg");
	addChild(image);
}

Every item have an id attributed to it. This id is the url path of the file, i.e. image.jpg. To files loaded from a request with parameters the id is formed by the url and the parameters, i.e mylist.php?product=car&color=red. Even if the request method is set to POST, the id of the file is set like a GET url.

The id is used to retrieve loaded data from the library.

var image:Bitmap = DataLib.getData("image.jpg");
addChild(image);

You can also pass an URLRequest as parameter to the getData method.

var request:URLRequest();
request.url = "mylist.php";
 
var variables:URLVariables = new URLVariables();
variables.product = "car";
variables.color = "red";
 
request.data = variables;
 
request.method = URLRequestMethod.POST;
 
var xml:XML = new XML(DataLib.getData(request));

The id is also used to remove, change priority and delete itens from the queue or delete itens from the library.

Download the source code and docs at google code:
http://code.google.com/p/datalib/

Check the documntation here:
http://www.andreanaya.com/datalib/doc/

You can download an usage example here:
http://www.andreanaya.com/datalib/DataLib_example.zip

2 Responses to “DataLib – New release of the loader class”

  1. Bruno Soares says:

    Aee Anaya, muito bom!
    Congrats

Leave a Reply