开发者

Flex load modules READY event not fired, why?

Hello I have this very simple mxml example.

Using flex builder with flex sdk 3.4.0 if the module is remote like http://edofiles.s3.amazonaws.com/calculator.swf

The READY event is never fired, i don't understand why.

Do you get the same behavior ?

Original code is from http://lowpitch.com/blog/modulemanager-and-imoduleinfo-loading-flex-modules-dynamically/#comment-4396

Any clues ?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    applicationComplete="initApp()">
    <mx:Script>
        <![CDATA[
            import mx.modules.Module;
            import mx.events.ModuleEvent;
            import mx.modules.ModuleManager;
            import mx.modules.IModuleInfo;

          protected var _moduleInfo:IModuleInfo;

            private function initApp():void {
               addToLog ("Application initialised");
               // create the module - note, we're not loading it yet
                _moduleInfo = ModuleManager.getModule("http://edofiles.s3.amazonaws.com/calculator.swf");
               // add some listeners
               _moduleInfo.addEventListener(ModuleEvent.READY, onModuleReady);
               _moduleInfo.addEventListener(ModuleEvent.SETUP, onModuleSetup);
               _moduleInfo.addEventListener(ModuleEvent.UNLOAD, onModuleUnload);
     开发者_如何转开发          _moduleInfo.addEventListener(ModuleEvent.PROGRESS, onModuleProgress);
            }

            protected function getModuleInfo () : IModuleInfo {
               // return the module info
               return _moduleInfo;
            }

            /**
             * Adds output to the log
             **/
            protected function addToLog (msg:String) : void {
                log.text += msg + "\n";
                // scroll to the bottom on the next frame
                callLater(scrollToBottom);

            }

            protected function scrollToBottom () : void {
                // scroll to the bottom
                log.verticalScrollPosition = log.maxVerticalScrollPosition;
            }

            /**
            * Called when the "load" button is pressed
            *
            * Starts loading the module - when the module has been
            * loaded, an instance of the module will be created
            * and added to the tile control
            *
            **/
            private function loadModule () : void {
                addToLog ("Loading module");
                // load the module
                getModuleInfo().load();
            }

            /**
             * Called when the "unload" button is pressed
             *
             * Removes all the instances of the module from the
             * tile control, then unloads the module
             *
             */
            private function unloadModule () : void {
                addToLog ("Unloading module");
                // clear out all the the instances
                // of the module from the tile
                tile.removeAllChildren();
                // unload the module
                getModuleInfo().release();
                //getModuleInfo().un
            }

            /**
            * Handler for the ModuleEvent.PROGRESS event
            **/
            protected function onModuleProgress (e:ModuleEvent) : void {
                addToLog ("ModuleEvent.PROGRESS received: " + e.bytesLoaded + " of " + e.bytesTotal + " loaded.");
            }

            /**
            * Handler for the ModuleEvent.SETUP event
            **/
            private function onModuleSetup (e:ModuleEvent) : void {
                addToLog ("ModuleEvent.SETUP received");
                // cast the currentTarget
                var moduleInfo:IModuleInfo = e.currentTarget as IModuleInfo;
                addToLog ("Calling IModuleInfo.factory.info ()");
                // grab the info and display information about it
                var info:Object = moduleInfo.factory.info();
                for (var each:String in info) {
                    addToLog ("     " + each + " = " + info[each]);
                }

            }

            /**
            * Handler for the ModuleEvent.READY event
            **/
            private function onModuleReady (e:ModuleEvent):void {
                addToLog ("ModuleEvent.READY received");
                // cast the currentTarget
                var moduleInfo:IModuleInfo = e.currentTarget as IModuleInfo;
                // Add an instance of the module's class to the
                // display list.
                addToLog ("Calling IModuleInfo.factory.create ()");
                tile.addChild( moduleInfo.factory.create () as Module);
                addToLog ("SomeModule instance created and added to Display List");
            }

            /**
            * Handler for the ModuleEvent.UNLOAD event
            **/
            public function onModuleUnload (e:ModuleEvent) : void {
                addToLog ("ModuleEvent.UNLOAD received");
            }

        ]]>
    </mx:Script>

    <mx:HBox width="100%" height="100%">
        <mx:Tile id="tile" width="100%" height="100%" />
           <mx:TextArea id="log" width="100%" height="100%"/>
    </mx:HBox>


    <mx:ApplicationControlBar>
        <mx:Button label="Load" click="loadModule ()" />
        <mx:Button label="Unload" click="unloadModule ()"/>
    </mx:ApplicationControlBar>

</mx:Application>


I had this behavior today. Even though it's not directly related to what the poster asked, this topic comes up pretty high in google search, so I want to help out others who stumble here.

I found the solution at the following blog: http://www.joshuaostrom.com/2008/08/14/flex-modules-watch-your-scope/

The problem is that ModuleLoader automatically garbage collects when you call load(), so if you're not extra careful with your references, your listeners can get destroyed.


The reason can be simple and funny. Try taht: first addEventListener code, next getModule().


it can also happen because you try to load a module compiled for release with an application compiled for debug. This has happened to me before. both app and modules must match debug/debug or release/release.

if they do not match, you will get progress events, and "setup" events, but never a ready event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜