views:

43

answers:

2

Hi, I am developing an AIR app. In the main app I have a module loader 'mainModuleLoader'. I am creating modules as separate mxml files using the <mx:Module> tag. And I am loading such a module in mainModuleLoader dynamically using actionscript. Everything works fine.

For the module which I am creating as mxml files, I would like to know how to add an unload event handler. So that, whenever I call mainModuleLoader.unload(), the unload event hander is triggered from inside the module.mxml. I have tried the following without any success.

<mx:Module creatingComplete="init()" unload="unloadHandler()"/>
<mx:Module creatingComplete="init()" remove="unloadHandler()"/>

The problem with the second statement above is that it triggers the unloadHandler even if some child is removed inside the module. I have also tried to add the following in the action script.

private function init()
{
  this.addEventListener(ModuleEvent.UNLOAD, unloadHandler);
}

But, it doesn't trigger on unloading the module. Kindly let me know if I am going wrong somewhere or if there is any other method to attach the event handlers. Thanks!!

A: 

I think you have to handle the unload event of your mainModuleLoader:

<mx:ModuleLoader id="mainModuleLoader" unload="unloadHandler()"/>
splash
Hey Splash, I cannot do that as different modules will have different unload functions defined internally. Some modules might not have unload functions also. So, I thought it's better for the module to call its own unload handlers if it has one.
Goje87
@Goje87, I cannot find that `Module` has an `unload` event.
splash
+1  A: 

You are part way there with the remove... just check the event.target is the module before doing your work, it'll ignore the other ones then.

Gregor Kiddie
Thanks Gregor!! This one resolves my issue. :) in the remove handler i am simply using if(!(evt.target is myModule)) return;
Goje87