Using Modules with Preloaders
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.
| Print article | This entry was posted by Danny Kopping on April 11, 2009 at 10:29 pm, and is filed under ActionScript 3.0, Flex. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 2 years ago
Thanks!
This was really help.
Keep up the good works!
about 2 years ago
Hi,i want to ask.Does using moduleManager add swf size?Also how to do it by using moduleLoader instead of using modulemanager..Thanks..
about 2 years ago
incompatible override error found on
override private function loadModule():void
how to fix this error, please help.
TIA
about 1 year ago
I got the error to go away, but the proader functionality doesn’t work for me yet.
I changed
override public function loadModule():void
to
override public function loadModule(url:String = null, bytes:ByteArray = null):void
about 1 year ago
Hi Randall
Sorry i didn’t reply… The error you were getting comes from the Flex 3.4 SDK i think… They adjusted the loadModule function. I was probably using the Flex 3.3 SDK when i wrote that post.
Thanks!
about 5 months ago
i changed the override to loadModule(url:String = null, bytes:ByteArray = null):void like Randall. But the solution dont work for me also.
about 5 months ago
What error are you getting Frank?
about 5 months ago
Hello,
I am using it in my Flex 4 project, but it does not work. It shows the progress bar but the rest of the application is not shown. It is as if the application is hanged and the Progress Bar does not Unload.
Any idea about this? Thanks
about 5 months ago
Have you used an event handler for the error event? What error do you get?