Safe Module Initialization

Modules are really nice for our particular application because we already have 100s of screens and need to be able to scale to have 1000s of screens some day. One of the things I looked for but did not find immediately in the Module framework was a way for the main application loading the Module to know when the loaded Module is safe for communication.

Most components dispatch a creationComplete event. This event can be used to perform whatever post-processing needs to be performed after the component is completely created and initialized (including all contained children).

The ModuleLoader is the component used to load in Modules. The ModuleLoader does not expose a creationComplete event. The ModuleLoader does however expose a ready event. The ready event indicates the contained Module has been loaded and instantiated. We can use this information to get at our newly loaded Module's creationComplete event registration in an effort to know when it is safe to post-process the Module. Here is some code in the Main application that sets this up:

// Main.mxml has a ModuleLoade named moduleLoader. In Main.mxml, define the
// moduleLoader's “ready” event property to be moduleLoader_ready(event:ModuleEvent)
//
// Then define moduleLoader_ready as:
//
protected function moduleLoader_ready(event:ModuleEvent):void
{
   
this.moduleLoader.child.addEventListener(FlexEvent.CREATION_COMPLETE, 
      
moduleLoader_child_creationComplete);
}

// The above code will register an event handler for the creationComplete event
// fired from the content loaded into the Module
//
protected function moduleLoader_child_creationComplete(event:FlexEvent):void
{
   
this.moduleLoader.child.removeEventListener(FlexEvent.CREATION_COMPLETE,
      moduleLoader_child_creationComplete);

   
// at this point we know the Module has been loaded and has had it
   
// lifecycle run up to creationComplete. It should now be safe to do
   
// normal component post-processing on the Module.
   
// We require all of our Modules to implement a standard contract, IScreen. 
   
var iScreen:IScreen = IScreen(this.moduleLoader.child);

   
iScreen.context = this.getCurrentContext();
   
iScreen.doSomethingStandard();

   
// etc
}

Within our Module we sometimes wish to query the Main application. Again, we use a contract, this time called IScreenContainer. As we have some commonality amongst screens (Modules), we have base screen class. There is a property on the base screen class to get the IScreenContainer:

protected function get screenContainer():IScreenContainer
{
   
return IScreenContainer(Application.application);
}

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

 Name (required)

 Email (will not be published) (required)

Your comment is 0 characters limited to 3000 characters.