Posts tagged preloaders
Using Modules with Preloaders
Apr 11th
Here’s a quick and dirty way to show a preloader while your module loads:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | <?xml version="1.0" encoding="utf-8"?> <mx:ModuleLoader xmlns:mx="http://www.adobe.com/2006/mxml" currentState="loading"> <mx:states> <mx:State name="loading"> <mx:SetStyle name="horizontalAlign" value="center"/> <!-- Align progress bar to center --> <mx:SetStyle name="verticalAlign" value="middle"/> <!-- Align progress bar to middle --> <mx:AddChild position="lastChild"> <mx:ProgressBar id="progressBar" labelPlacement="center" mode="manual"/> </mx:AddChild> </mx:State> </mx:states> <mx:Script> <![CDATA[ import mx.modules.IModuleInfo; import mx.events.FlexEvent; import mx.modules.ModuleManager; import mx.events.ModuleEvent; private var module:IModuleInfo; override public function loadModule():void { if (url == null) return; if (child) return; if (module) return; dispatchEvent(new FlexEvent(FlexEvent.LOADING)); module = ModuleManager.getModule(url); module.addEventListener(ModuleEvent.PROGRESS, moduleProgressHandler); module.addEventListener(ModuleEvent.SETUP, moduleSetupHandler); module.addEventListener(ModuleEvent.READY, moduleReadyHandler); module.addEventListener(ModuleEvent.ERROR, moduleErrorHandler); module.addEventListener(ModuleEvent.UNLOAD, moduleUnloadHandler); module.load(applicationDomain); } private function moduleProgressHandler(event:ModuleEvent):void { dispatchEvent(event); try { progressBar.setProgress(event.bytesLoaded / event.bytesTotal, 1); } catch(e:Error) { } } private function moduleSetupHandler(event:ModuleEvent):void { // Not ready for creation yet, but can call factory.info(). dispatchEvent(event); } private function moduleReadyHandler(event:ModuleEvent):void { child = module.factory.create() as DisplayObject; dispatchEvent(event); if (child) { var p:DisplayObjectContainer = parent; // p.removeChild(this); addChild(child); setStyle('horizontalAlign', ''); // Reset horizontalAlign setStyle('verticalAlign', ''); // Reset verticalAlign currentState = ''; } } private function moduleErrorHandler(event:ModuleEvent):void { unloadModule(); dispatchEvent(event); } private function moduleUnloadHandler(event:ModuleEvent):void { dispatchEvent(event); } override public function unloadModule():void { if (child) { removeChild(child); child = null; } if (module) { module.removeEventListener(ModuleEvent.PROGRESS, moduleProgressHandler); module.removeEventListener(ModuleEvent.SETUP, moduleSetupHandler); module.removeEventListener(ModuleEvent.READY, moduleReadyHandler); module.removeEventListener(ModuleEvent.ERROR, moduleErrorHandler); module.unload(); module.removeEventListener(ModuleEvent.UNLOAD, moduleUnloadHandler); module = null; } } ]]> </mx:Script> </mx:ModuleLoader> |
Or you can download the file here.