Posted By

Bonky on 07/28/10


Tagged

bulkloaderproxypuremvcasset


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

Demian
snick
copic
mediapoint


BulkLoader Proxy for PureMVC


 / Published in: ActionScript 3
 

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

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

  1. package tv.stinkdigital.puremvcbase.model
  2. {
  3. import br.com.stimuli.loading.BulkLoader;
  4. import br.com.stimuli.loading.BulkProgressEvent;
  5. import br.com.stimuli.loading.loadingtypes.LoadingItem;
  6.  
  7. import flash.events.ErrorEvent;
  8. import flash.events.Event;
  9.  
  10. import org.puremvc.as3.interfaces.IProxy;
  11. import org.puremvc.as3.patterns.proxy.Proxy;
  12.  
  13. import tv.stinkdigital.puremvcbase.ApplicationFacade;
  14.  
  15. public class BulkLoaderProxy extends Proxy implements IProxy
  16. {
  17. public static const NAME:String = "BulkLoaderProxy";
  18.  
  19. public function BulkLoaderProxy(data:Object=null)
  20. {
  21. super(NAME, data);
  22. }
  23.  
  24. public function addLoader(loaderId:String):void
  25. {
  26. var bulkLoader:BulkLoader = new BulkLoader( loaderId );
  27.  
  28. bulkLoader.addEventListener(BulkLoader.COMPLETE, onComplete);
  29.  
  30. bulkLoader.addEventListener(BulkLoader.PROGRESS, onProgress);
  31.  
  32. bulkLoader.addEventListener(BulkLoader.ERROR, onError);
  33. bulkLoader.addEventListener(BulkLoader.SECURITY_ERROR, onError);
  34. }
  35.  
  36. 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
  37. {
  38. var props:Object = { id:itemId, type:type, weight:weight, priority:priority, maxTries:maxTries, preventCaching:preventCaching, checkPolicyFile:checkPolicyFile, headers:headers };
  39. getLoader( loaderId ).add( url, props );
  40. }
  41.  
  42.  
  43.  
  44. public function getLoader(loaderId:String):BulkLoader
  45. {
  46. return BulkLoader.getLoader( loaderId );
  47. }
  48.  
  49. public function getItem(loaderId:String, itemId:String ):LoadingItem
  50. {
  51. return getLoader( loaderId ).get( itemId );
  52. }
  53.  
  54. public function getItemContent(loaderId:String, itemId:String, clearMemory:Boolean = false ):*
  55. {
  56. return getLoader( loaderId ).getContent( itemId, clearMemory );
  57. }
  58.  
  59. public function startLoader(loaderId:String):void
  60. {
  61. getLoader( loaderId ).start();
  62. }
  63.  
  64. public function clearLoader(loaderId:String):void
  65. {
  66. getLoader( loaderId ).clear();
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. protected function onComplete(event:Event):void
  74. {
  75. sendNotification( ApplicationFacade.BULKLOADER_COMPLETE, (event.target as BulkLoader) );
  76. }
  77.  
  78. protected function onProgress(event:BulkProgressEvent):void
  79. {
  80. sendNotification( ApplicationFacade.BULKLOADER_PROGRESS, (event.target as BulkLoader) );
  81. }
  82.  
  83. protected function onError(event:ErrorEvent):void
  84. {
  85. sendNotification( ApplicationFacade.BULKLOADER_ITEM_ERROR, (event.target as LoadingItem) );
  86. }
  87.  
  88.  
  89. }
  90. }
  91.  
  92. //Usage as so (i created a BulkLoaderMediator as a template):
  93.  
  94.  
  95. package tv.stinkdigital.puremvcbase.view
  96. {
  97. import br.com.stimuli.loading.BulkLoader;
  98. import br.com.stimuli.loading.loadingtypes.LoadingItem;
  99.  
  100. import flash.display.Bitmap;
  101.  
  102. import org.osflash.thunderbolt.Logger;
  103. import org.puremvc.as3.interfaces.IMediator;
  104. import org.puremvc.as3.interfaces.INotification;
  105. import org.puremvc.as3.patterns.mediator.Mediator;
  106.  
  107. import tv.stinkdigital.puremvcbase.ApplicationFacade;
  108. import tv.stinkdigital.puremvcbase.model.BulkLoaderProxy;
  109. import tv.stinkdigital.puremvcbase.view.components.LoadingBar;
  110.  
  111. public class BulkLoaderMediator extends Mediator implements IMediator
  112. {
  113. public static const NAME:String = "BulkLoaderMediator";
  114.  
  115. protected var bulkLoaderProxy:BulkLoaderProxy;
  116. protected var loaderId:String = "bulkLoad";
  117.  
  118. public function BulkLoaderMediator(viewComponent:Object)
  119. {
  120. super(NAME, viewComponent);
  121. }
  122.  
  123. override public function onRegister():void
  124. {
  125. // add component to stage
  126. sendNotification(ApplicationFacade.ADD_TO_STAGE, viewComponent);
  127.  
  128. // cache a reference to frequently used proxies
  129. bulkLoaderProxy = facade.retrieveProxy( BulkLoaderProxy.NAME ) as BulkLoaderProxy;
  130.  
  131. bulkLoaderProxy.addLoader( loaderId );
  132.  
  133. bulkLoaderProxy.addItem( loaderId, "http://example.com/1.jpg", "image1", BulkLoader.TYPE_IMAGE, 100 );
  134. bulkLoaderProxy.addItem( loaderId, "http://example.com/2.jpg", "image2", BulkLoader.TYPE_IMAGE, 100 );
  135. bulkLoaderProxy.addItem( loaderId, "http://example.com/3.jpg", "image3", BulkLoader.TYPE_IMAGE, 100 );
  136.  
  137. bulkLoaderProxy.startLoader( loaderId );
  138. }
  139.  
  140. override public function listNotificationInterests():Array
  141. {
  142. return [
  143. ApplicationFacade.BULKLOADER_PROGRESS,
  144. ApplicationFacade.BULKLOADER_COMPLETE,
  145. ApplicationFacade.BULKLOADER_ITEM_ERROR
  146. ];
  147. }
  148.  
  149. override public function handleNotification(note:INotification):void
  150. {
  151. switch ( note.getName() )
  152. {
  153. case ApplicationFacade.BULKLOADER_PROGRESS:
  154.  
  155. var bulkLoader:BulkLoader = note.getBody() as BulkLoader;
  156. if( bulkLoader.name == loaderId )
  157. {
  158. loadingBar.update( bulkLoader.weightPercent );
  159. }
  160. break;
  161.  
  162. case ApplicationFacade.BULKLOADER_COMPLETE:
  163.  
  164. if( (note.getBody() as BulkLoader).name == loaderId )
  165. {
  166. Logger.info( "Bulk Load Complete" );
  167.  
  168. loadingBar.hide();
  169.  
  170.  
  171. var img1:Bitmap = bulkLoaderProxy.getItemContent( loaderId, "image1" ) as Bitmap;
  172. sendNotification( ApplicationFacade.ADD_TO_STAGE, img1 );
  173.  
  174. var img2:Bitmap = bulkLoaderProxy.getItemContent( loaderId, "image2" ) as Bitmap;
  175. img2.x = img1.width;
  176. sendNotification( ApplicationFacade.ADD_TO_STAGE, img2 );
  177.  
  178. var img3:Bitmap = bulkLoaderProxy.getItemContent( loaderId, "image3" ) as Bitmap;
  179. img3.x = img2.x + img1.width;
  180. sendNotification( ApplicationFacade.ADD_TO_STAGE, img3 );
  181.  
  182.  
  183. }
  184.  
  185. break;
  186.  
  187. case ApplicationFacade.BULKLOADER_ITEM_ERROR:
  188.  
  189.  
  190. Logger.error( "BULKLOADER_ITEM_ERROR" );
  191.  
  192. break;
  193.  
  194. }
  195. }
  196.  
  197. protected function get loadingBar():LoadingBar
  198. {
  199. return viewComponent as LoadingBar;
  200. }
  201.  
  202. }
  203. }

Report this snippet  

You need to login to post a comment.