I was having some issues with a pureMVC app I've been building with the new release of Fabrication for pureMVC. The issue was my module wasn't being loaded properly or didn't have a router. So I decided to write a really simple flex project to test it outside of my bigger project.
I created a FlexApplication (that uses fabrication)
<?xml version="1.0" encoding="utf-8"?>
<fab:FlexApplication
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
xmlns:fab="org.puremvc.as3.multicore.utilities.fabrication.components.*">
<mx:Script>
<![CDATA[
import com.bti.sandbox.FabricationTest.control.ShellStartupCommand;
override public function getStartupCommand():Class
{
return ShellStartupCommand;
}
]]>
</mx:Script>
</fab:FlexApplication>
The statup command is super simple just for testing.
package com.bti.sandbox.FabricationTest.control
{
import mx.events.ModuleEvent;
import mx.modules.IModuleInfo;
import mx.modules.ModuleManager;
import mx.utils.ObjectUtil;
import org.puremvc.as3.multicore.interfaces.INotification;
import org.puremvc.as3.multicore.utilities.fabrication.components.FlexApplication;
import org.puremvc.as3.multicore.utilities.fabrication.components.FlexModule;
import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
public class ShellStartupCommand extends SimpleFabricationCommand
{
override public function execute(note:INotification):void
{
// lets test this thing by loading a module
var module : IModuleInfo= ModuleManager.getModule("TestModule.swf");
module.addEventListener( ModuleEvent.READY, moduleReadyListener);
module.addEventListener( ModuleEvent.ERROR, handleError);
module.load();
}
public function moduleReadyListener( event : ModuleEvent) : void
{
var moduleInstance : FlexModule = event.module.factory.create() as FlexModule;
trace("module instance is " + moduleInstance);
moduleInstance.router = (this.fabFacade.getApplication() as FlexApplication).router;
moduleInstance.initialize();
}
public function handleError( event : ModuleEvent) :void
{
trace("error " + ObjectUtil.toString( event));
}
}
}
the simple module I'm using for testing :
<?xml version="1.0" encoding="utf-8"?>
<FlexModule xmlns="org.puremvc.as3.multicore.utilities.fabrication.components.*" xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%">
<mx:Label text="I am the test module"/>
<mx:Script>
<![CDATA[
import com.bti.sandbox.TestModule.control.TestModuleStartupCommand;
override public function getStartupCommand():Class
{
return TestModuleStartupCommand;
}
]]>
</mx:Script>
</FlexModule>
</code>
And finally the startup command for the simple module
package com.bti.sandbox.TestModule.control
{
import org.puremvc.as3.multicore.interfaces.INotification;
import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
public class TestModuleStartupCommand extends SimpleFabricationCommand
{
override public function execute( note:INotification ):void
{
trace("TestModuleStartupCommand started!!!");
}
}
}
In testing the "moduleReadyListener" is never called, and the "handleError" is never called. I tested the app with a Charles - http proxy in the browser and the module was being loaded, but the READY event was never being fired.
I was googling in frustration and found this note so I made a really small change to the applications startup command, I made the module variable a member of the class instead of local to the method and viola, it works.
package com.bti.sandbox.FabricationTest.control
{
import mx.events.ModuleEvent;
import mx.modules.IModuleInfo;
import mx.modules.ModuleManager;
import mx.utils.ObjectUtil;
import org.puremvc.as3.multicore.interfaces.INotification;
import org.puremvc.as3.multicore.utilities.fabrication.components.FlexApplication;
import org.puremvc.as3.multicore.utilities.fabrication.components.FlexModule;
import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
public class ShellStartupCommand extends SimpleFabricationCommand
{
private var module : IModuleInfo;
override public function execute(note:INotification):void
{
// lets test this thing by loading a module
module = ModuleManager.getModule("TestModule.swf");
module.addEventListener( ModuleEvent.READY, moduleReadyListener);
module.addEventListener( ModuleEvent.ERROR, handleError);
module.load();
}
public function moduleReadyListener( event : ModuleEvent) : void
{
var moduleInstance : FlexModule = event.module.factory.create() as FlexModule;
trace("module instance is " + moduleInstance);
moduleInstance.router = (this.fabFacade.getApplication() as FlexApplication).router;
moduleInstance.initialize();
}
public function handleError( event : ModuleEvent) :void
{
trace("error " + ObjectUtil.toString( event));
}
}
}
This type of bug is infurating, but at least I now have a fix :)