Using PureMVC Loadup utility with Flash IDE

PureMVC Loadup for Flash demo screenshot

The Loadup library is a PureMVC utility intended to add support for Lazy Loading in PureMVC applications. It can also be used to load assets and report progress during loading.

I recently had to use the assetloader package of the PureMVC Loadup utility in a pure ActionScript project. When I finished to integrate it in my project, I realized that my application took an unwanted 120KB file size. After a little exploration, I found some direct references to mx.core.UIComponent or mx.controls.Image in the AssetOfTypeImage, AssetOfTypeSwf and AssetOfTypetext classes of the PureMVC Loadup utility.

As you may know, UIComponent is the base class for all Flex SDK components and is required when you compile a Flex project. As it requires a lot of dependencies and take something like 120KB, when file size is critical, a good choice is not to use the Flex SDK UI components in your project. Another side effect of using it is that it prevents to compile the project from the Flash IDE as Flash IDE currently can't compile most of the Flex SDK dependencies which is quite frustrating as PureMVC is intended to be fully Flash/Flex compatible.

When I reported this problem on the PureMVC forums some users asked for a solution, I promised to blog mine. So here it is.

Thanks to Philip Sexton, its author, the Loadup utility is pretty well made. It is really easy to change the default asset classes to remove any Flex dependencies. It never requires to modify the utility itself, only its implementation.

The AssetOfType... classes only have references in the default AssetTypeMap. I had to create a new class named FlashAssetTypeMap which is a slightly modified copy of AssetTypeMap without its reference to AssetOfType... classes. Instead I created three AssetOfTypeFlashImage, AssetOfTypeFlashSwf and AssetOfTypeFlashtext which don't need references to the Flex SDK.

Here is main changes brought by FlashAssetTypeMap:

protected function defaultMap():Object
{
    var obj :Object = new Object();
    obj[ Loadup.IMAGE_ASSET_TYPE ] = [ AssetOfTypeFlashImage, AssetLoadByLoader ];
    obj[ Loadup.SWF_ASSET_TYPE ] = [ AssetOfTypeFlashSwf, AssetLoadByLoader ];
    obj[ Loadup.TEXT_ASSET_TYPE ] = [ AssetOfTypeFlashText, AssetLoadByURLLoader ];
    return obj;
}

Where we had this in the original AssetTypeMap:

protected function defaultMap():Object
{
    var obj :Object = new Object();
    obj[ Loadup.IMAGE_ASSET_TYPE ] = [ AssetOfTypeImage, AssetLoadByLoader ];
    obj[ Loadup.SWF_ASSET_TYPE ] = [ AssetOfTypeSwf, AssetLoadByLoader ];
    obj[ Loadup.TEXT_ASSET_TYPE ] = [ AssetOfTypeText, AssetLoadByURLLoader ];
    return obj;
}

And, as an example, the newly created AssetOfTypeFlashImage:

 
public class AssetOfTypeFlashImage
	extends AssetBase
	implements IAsset
{
	public function AssetOfTypeFlashImage( url:String )
	{
		super(url);
	}
 
	public function get type():String
	{
		return Loadup.IMAGE_ASSET_TYPE;
	}
 
	public function get bitmap():Bitmap
	{
		return data as Bitmap;
	}
}
 

And the original AssetOfTypeImage that had the unwanted Flex SDK references:

public class AssetOfTypeImage
	extends AssetBase
	implements IAssetForFlex
{
	private var _uiComponent :UIComponent;
 
	public function AssetOfTypeImage( url :String )
	{
		super( url );
	}
 
	public function get type() :String
	{
		return Loadup.IMAGE_ASSET_TYPE;
	}
 
	public function get uiComponent() :UIComponent
	{
		if ( ! data )
		        return null;
 
		if ( ! _uiComponent )
		{
			var img :Image = new Image();
			img.load( data);
			_uiComponent = img;
		}
		return _uiComponent;
	}
}
 

Now, I'm sure that we can update the Loadup utility to Loadup 2.1 to extract the default map from the AssetTypeMap to a class. Or probably a better idea to create a different flex and a flash package to have default assets and asset type map for each.

You can launch the live demo, download or browse the sources.

Note that I use a specific configuration that allows to easily compile the same project in Flash Builder and Flash CS3. Don't use this configuration in your real Flash Builder projects, the SWC compiled from Flash IDE and used for graphical assets in Flash Builder also contains the whole application code. You better have to generate a SWC from Flash with graphical assets only. Also note that as I use Flash CS3 to compile the project, you will find lib source instead of SWC as Flash CS3 compiler is unable to use SWCs.

3 thoughts on “Using PureMVC Loadup utility with Flash IDE

  1. Pingback: Twitter Trackbacks for Tekool.net - Développeur Flash - Using PureMVC Loadup utility with Flash IDE [tekool.net] on Topsy.com

  2. Pingback: Twitted by AS3LibHash

  3. Pingback: Most Tweeted Articles by Flash Experts

Leave a Reply

Your email address will not be published. Required fields are marked *

*

  Am Not Spammer

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>