Friday, December 15, 2006

Flex Builder 2.01 and mx.modules

Ok, so pong exploration project delayed slightly.
I'm working with flex builder 2.01 and they have put quite a bit of work into mx.modules.
There weren't any examples anywhere, so I was left to explore the API documentation.

Most of my day was spent wrestling in IIS PHP MYSQL hell getting a wiki setup, but after I got that done, I devoted fulltime to replacing my old classloader flex hacks with mx.modules.

I just got them working. They were very foreign at first and I was cursing at not having better examples to work with, but now that I know how they're used, I can drive home victorious.

Some code for others who may be struggling with these puppies:

First I created a ModuleEventHandler to hold an instance of my global framework that I want to pass into the module with an init method later. (You're supposed to be able to do this by passing it to the create method, but I couldn't get that working yet).

public class ModuleEventHandler {
public var gui:SomeFrameworkInterface;
public function ModuleEventHandler(gui:SomeFrameworkInterface) { this.gui=gui; }
public function onModuleReady(e:ModuleEvent):void {
var i:IModuleInfo = ModuleManager.getModule(e.target.url);
Object(i.factory.create()).init(gui);
}
}

Then I created a generic loadModule function that will do the dirty work. All my modules will be suffixed with Wrapper.swf.

public function loadModule(name:String):void {
var m:ModuleLoader = new ModuleLoader();
m.url = name+"Wrapper.swf";
m.loadModule();
m.addEventListener(mx.events.ModuleEvent.READY ,(new ModuleEventHandler(gui)).onModuleReady);
}

the SomeWrapper class looks like this

public class SomeWrapper extends mx.modules.ModuleBase implements IFlexModuleFactory
{
public var gui:SomeFrameworkInterface;
public function init(gui:SomeFrameworkInterface) { this.gui=gui;
//other code here to modify the gui how you want to using the module stuff
// most likely dispatch some event on it that it reacts to.
}
public function create(... parameters):Object { return new SomeWrapper(); }
public function info():Object { return {}; }
}

In my interface I have an accordian that the module adds itself to, and its working so I'm stoked =)

4 comments:

Unknown said...

Hey Seth, way to get a jump on 2.0.1

I've been playing around with the mx.modules stuff over the few week or so, its pretty wicked.

You should watch this blog, he has the 411 on modules http://blogs.adobe.com/rgonzalez/2006/06/modular_applications_part_1.html

Seth Caldwell said...

yea, I got his pp notes from some presentation he gave a while back, thats how I got some of the runtime loading stuff going that I demod when you guys were here.

He doesn't post frequently enough tho so I don't regularly check it =p

Unknown said...

Hi Seth
I find it strange that you combine the use of ModuleLoader and ModuleManager. Any reason you do not load through the IModuleInfo.load() function?
Also:
Do you really have to extend the IFlexModuleFactory in your module?
Don't you just get that interface through the IModuleInfo.factory anyway?
I am having a particular issue with loading modules. When my module contains (not necessarily at the top level) mxml declarations, those object do not instantiate (remain null) when I create module instances through the factory. They do instantiate though correctly in the ModuleLoader.child property. Have you encountered this?

Seth Caldwell said...

No, I haven't ran into what you described. And yes, you're right my post was a first go at the modules without having any guidance, just looking at the code when I received the new flex builder... so I did a few weird things to get modules loading that I probably didn't have to.
Its been a while, I changed companies and I'm not currently working with flex, though I might at some point in the future. I'm assuming you resolved your issue, could you post the solution in case anyone google's to here?