Return to Snippet

Revision: 29390
at July 28, 2010 08:04 by Bonky


Initial Code
package tv.stinkdigital.puremvcbase.model
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import br.com.stimuli.loading.loadingtypes.LoadingItem;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;

	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import tv.stinkdigital.puremvcbase.ApplicationFacade;

	public class BulkLoaderProxy extends Proxy implements IProxy
	{
		public static const NAME:String = "BulkLoaderProxy";
		
		public function BulkLoaderProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function addLoader(loaderId:String):void
		{
			var bulkLoader:BulkLoader = new BulkLoader( loaderId );
			
			bulkLoader.addEventListener(BulkLoader.COMPLETE, onComplete);
            
            bulkLoader.addEventListener(BulkLoader.PROGRESS, onProgress);
            
            bulkLoader.addEventListener(BulkLoader.ERROR, onError);
            bulkLoader.addEventListener(BulkLoader.SECURITY_ERROR, onError);
		}
		
		public function addItem(loaderId:String, url:String, itemId:String, type:String = null, weight:int = 0, priority:int=0, maxTries:uint=1, preventCaching:Boolean = false, checkPolicyFile:Boolean = false, headers:Array=null ):void
		{
			var props:Object = { id:itemId, type:type, weight:weight, priority:priority, maxTries:maxTries, preventCaching:preventCaching, checkPolicyFile:checkPolicyFile, headers:headers };
			getLoader( loaderId ).add( url, props );
		}
		
		
		
		public function getLoader(loaderId:String):BulkLoader
		{
			return BulkLoader.getLoader( loaderId );
		}
		
		public function getItem(loaderId:String, itemId:String ):LoadingItem
		{
			return getLoader( loaderId ).get( itemId );
		}
		
		public function getItemContent(loaderId:String, itemId:String, clearMemory:Boolean = false ):*
		{
			return getLoader( loaderId ).getContent( itemId, clearMemory );
		}
		
		public function startLoader(loaderId:String):void
		{
			getLoader( loaderId ).start();
		}
		
		public function clearLoader(loaderId:String):void
		{
			getLoader( loaderId ).clear();
		}
		
		
		
		
		
		protected function onComplete(event:Event):void
		{
			sendNotification( ApplicationFacade.BULKLOADER_COMPLETE, (event.target as BulkLoader) );
		}
		
		protected function onProgress(event:BulkProgressEvent):void
		{
			sendNotification( ApplicationFacade.BULKLOADER_PROGRESS, (event.target as BulkLoader) );
		}
		
		protected function onError(event:ErrorEvent):void
		{
			sendNotification( ApplicationFacade.BULKLOADER_ITEM_ERROR, (event.target as LoadingItem) );
		}
		
		
	}
}

//Usage as so (i created a BulkLoaderMediator as a template):


package tv.stinkdigital.puremvcbase.view
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.loadingtypes.LoadingItem;
	
	import flash.display.Bitmap;
	
	import org.osflash.thunderbolt.Logger;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import tv.stinkdigital.puremvcbase.ApplicationFacade;
	import tv.stinkdigital.puremvcbase.model.BulkLoaderProxy;
	import tv.stinkdigital.puremvcbase.view.components.LoadingBar;

	public class BulkLoaderMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "BulkLoaderMediator";
		
		protected var bulkLoaderProxy:BulkLoaderProxy;
		protected var loaderId:String = "bulkLoad";
		
		public function BulkLoaderMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			// add component to stage
            sendNotification(ApplicationFacade.ADD_TO_STAGE, viewComponent);
            
            // cache a reference to frequently used proxies
            bulkLoaderProxy = facade.retrieveProxy( BulkLoaderProxy.NAME ) as BulkLoaderProxy;           
            
           	bulkLoaderProxy.addLoader( loaderId );
			
			bulkLoaderProxy.addItem( loaderId, "http://example.com/1.jpg", "image1", BulkLoader.TYPE_IMAGE, 100 );
			bulkLoaderProxy.addItem( loaderId, "http://example.com/2.jpg", "image2", BulkLoader.TYPE_IMAGE, 100 );
			bulkLoaderProxy.addItem( loaderId, "http://example.com/3.jpg", "image3", BulkLoader.TYPE_IMAGE, 100 );
			
			bulkLoaderProxy.startLoader( loaderId );
		}
		
		override public function listNotificationInterests():Array
		{
			return [ 
					ApplicationFacade.BULKLOADER_PROGRESS,
					ApplicationFacade.BULKLOADER_COMPLETE,
					ApplicationFacade.BULKLOADER_ITEM_ERROR
                   ];
		}
		
		override public function handleNotification(note:INotification):void
		{
			switch ( note.getName() )
      		{
	          	case ApplicationFacade.BULKLOADER_PROGRESS:
	          		
	          		var bulkLoader:BulkLoader = note.getBody() as BulkLoader;
	          		if( bulkLoader.name == loaderId )
	          		{
		          		loadingBar.update( bulkLoader.weightPercent );
	          		}
	          		break;
	          		
	          	case ApplicationFacade.BULKLOADER_COMPLETE:
	          		
	          		if( (note.getBody() as BulkLoader).name == loaderId )
	          		{
		          		Logger.info( "Bulk Load Complete" );
		          		
		          		loadingBar.hide();
		          		
		          		
		          		var img1:Bitmap = bulkLoaderProxy.getItemContent( loaderId, "image1" ) as Bitmap; 
		          		sendNotification( ApplicationFacade.ADD_TO_STAGE, img1 );
		          		
		          		var img2:Bitmap = bulkLoaderProxy.getItemContent( loaderId, "image2" ) as Bitmap; 
		          		img2.x = img1.width;
		          		sendNotification( ApplicationFacade.ADD_TO_STAGE, img2 );
		          		
		          		var img3:Bitmap = bulkLoaderProxy.getItemContent( loaderId, "image3" ) as Bitmap; 
		          		img3.x = img2.x + img1.width;
		          		sendNotification( ApplicationFacade.ADD_TO_STAGE, img3 );
		          		
		          		
	          		}
	          		
	          		break;
	          		
	          	case ApplicationFacade.BULKLOADER_ITEM_ERROR:
	          		
	          		
	          		Logger.error( "BULKLOADER_ITEM_ERROR" );
	          		
	          		break;
	          		
      		}
		}
		
		protected function get loadingBar():LoadingBar
		{
            return viewComponent as LoadingBar;
        }
		
	}
}

Initial URL
http://forums.puremvc.org/index.php?topic=1292.0

Initial Description
Big thanks to StinkyIan for posting this at the PureMVC forums.

Initial Title
BulkLoader Proxy for PureMVC

Initial Tags


Initial Language
ActionScript 3