Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1] 2
Print
Author Topic: PureMVC + Modules  (Read 37658 times)
rockhowse
Newbie
*
Posts: 4


View Profile Email
« on: August 30, 2007, 12:14:23 »

There isn't really a lot of documentation on integrating PureMVC with Flex Modules so I thought I would post on these forums. (see attached for source)

The code that is included is based off Vic on Flex's Cairngorm + Modules example found here:

http://viconflex.blogspot.com/2007/05/can-cairngorm-and-modules-play-nice_12.html

This example is implemented using PureMVC and demonstrates how to use a "shell application" to load Modules that have been implemented using the PureMVC framework. It doesn't do anything fancy, but gives someone a good place to start when trying to dynamically link in PureMVC based modules into a Flex application.

The key to this example is that you can't reference the ApplicationFacade in the main application.  I had issues trying to load an ApplicationFacade in the root app and then have each module load it's own ApplicationFacade or register Notifications to either facade. There is something interesting going on with the Singleton pattern and how the main application -> modules scope is being handled.

I am going to start working on a more elegant solution that allows cross-module notifications but for the time being, this should get people started on the road to using Modules built on the PureMVC framework.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: August 30, 2007, 07:11:45 »

Nate,

I will check this out. You'd mentioned the modules thing earlier, and its been on my queue of things to address. Glad you posted your findings for us to get a start with.

I've got some ideas about the coordination issues surrounding the ApplicationFacade, but I'll look at what you've got here first.

Thanks,
-=Cliff>
Logged
rockhowse
Newbie
*
Posts: 4


View Profile Email
« Reply #2 on: September 18, 2007, 07:02:31 »

Eureaka! I got the cross-Module communication functionality working =) In my first example above, I had two modules that were loaded with their own independent ApplicationFacade. They each register their own command, but can't communicate in a cross-modular fassion. If I added an ApplicationFacade to the main app, all sorts of scoping issues came into play =P

Evidently this is similar to what happens when you try to marry Cairngorm + Modules as well because of the Singleton pattern being used.  In order to accomplish cross-module communication I used a technique that makes modules play nice with Cairngorm found here:

http://blog.digitalmaelstrom.net/2007/04/cairngorm-modules-trouble.html

For this new demo, I am able to get two modules loaded, each registering their own seperate commands. Each is now able to dispatch those commands in a cross-modular fassion using a centralized ApplicationFacade that was registered to each module upon creation.  This should really help someone out who wants to have an application that will be loading modules dynamically at runtime.

The key was the interface that lets my Modules be "Pure MVC Aware": IPureMVCAware. It basically forces a PureMVC aware module to implement the register/unregister functions upon load/unload from the main Application. I need to dig a bit deeper into getting Views/Models included into a demo as well as implement some cleaner unregister code, but for now this shows you how to send commands in a cross module fassion.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: September 19, 2007, 09:21:49 »

You rock, Rock. :)

I am sorry to say that I have been so swamped I haven't even had time to look into the previous example. I was waiting till I could clear a good amount of time to look into it.

It sounds like you've nailed the problem on the head, which is that you've got to set up a sort of 'master/slave' relationship between a module and the app that loads it.  The IPureMVCAware interface seems like a reasonable approach to the problem.

I really look forward to testing this out and seeing how it works. Can the loaded module also be written to run as a standalone application as well, following its own startup process depending upon whether its been loaded by another app or not?

-=Cliff>
Logged
rockhowse
Newbie
*
Posts: 4


View Profile Email
« Reply #4 on: September 19, 2007, 10:23:11 »

Unfortunatly, I don't think you can have a module switch between Module/Application at runtime because each inherits from a different base class =(.  If you try to run the module as a stand alone app I think it just gives you a grey screen.  From what I have seen you can replace <mx:Module/> with <mx:Application/> and it works pretty well. That's how I was originally testing my modules before I realized I could debug modules by loading the <moduleName>-debug.swf instead of the <moduleName>.swf from my main application.
Logged
arend
Newbie
*
Posts: 3


View Profile Email
« Reply #5 on: November 10, 2007, 05:09:47 »

This is exactly what I was looking for!

Thanks for posting your findings.

Arend
Logged
zilet
Courseware Beta
Jr. Member
***
Posts: 17


View Profile WWW Email
« Reply #6 on: January 14, 2008, 08:16:43 »

Hello,

  did you come to some good solution? I just started making modular app with flex and PureMVC and I ran into the singleton problem. The solution with passing the ApplicationFacade object is nice but it is not easy to handle registering and unregistering Mediator, commands and proxies.

I was thinking of making ModuleProxy that will handle registration. In this case the registration of meditarors will have to be called in the folowing manner:

:
moduleProxy.registerMediator(MediatorObject)
and similar for other registrationgs. ModuleProxy will be registered uppon moduleload and every time something is regitered through it, it will keep the info about registered medators, commands and proxies in array so on Module onload it can be easily unloaded.

This is the first thing that came across my mind and I don't really like the solution but it will work. The problem is that when somebody develops the modules he has to be aware of this so the module will not be so independent.

Any other idea?
Logged
zilet
Courseware Beta
Jr. Member
***
Posts: 17


View Profile WWW Email
« Reply #7 on: January 18, 2008, 11:00:27 »

I had some time to consider this problem and I have come to some solution which I think can do it. It has some drawbacks but allows unregistering of all commands, mediators and proxies when the module unloads.

 I have attached a source of the module test application. It simply loads 2 modules which both use puremvc.
 In main application I have created ModuleMediator which controls loading and unloading of modules. As a helper class i have used ModuleFacade (it is located in the "util" package). ModuleFacade is a singleton which contains reference to the ApplicationFacade object (which is not needed because it is also singleton but it was kinda clearer this way for me). ModuleFacade controls registering and unregistering of modules.
Every module has a ModuleContext associated with it and it is available through ModuleFacade. ModuleContext holds all module registered commands, proxies and mediators.

The only thing that one have to do when creating a module is to create its startupclass which will initialize startupcommand.
 
In this command we have to register other things through the reference of ModuleContext object.

I'll continue on testing the solution and I would be glad to hear some comments.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #8 on: January 18, 2008, 02:24:15 »

Zilet,

Sounds like a straightforward approach. I'll have a look shortly. Meanwhile, I know there are a lot of module-starved folks out there and we need an official utility and demo. This could be it. Lets hear what people have to say about this.

If it seems like a solid answer, would you like to have it folded into the site as an official utility, and separate demo? If so, have a look at this post over in the Contributor Central forum for an idea about how we're handling the community contribution aspect of the project now. http://forums.puremvc.org/index.php?topic=142.0

If so, please send me a copy by separate email, and I'll put you in the queue of projects to be created and get you repo access, etc.
-=Cliff>
Logged
tjiddy
Jr. Member
**
Posts: 10


View Profile Email
« Reply #9 on: January 21, 2008, 02:48:59 »

I am a PureMVC noob, but what do you think about moving the actual loading of the module into a LoadModuleCommand registered to listen to a LOAD_MODULE Notification, the body of the Notification can contain the module to load.  Then the ModuleMediator can do what other mediators do, and translate events into Notifications.  This would let you load modules from anywhere in your app (from other mediators, or commands)

Todd
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #10 on: January 21, 2008, 10:27:18 »

Right on.

-=Cliff>
Logged
zilet
Courseware Beta
Jr. Member
***
Posts: 17


View Profile WWW Email
« Reply #11 on: January 22, 2008, 02:59:10 »

you've got a good point there!

Logged
tjiddy
Jr. Member
**
Posts: 10


View Profile Email
« Reply #12 on: January 22, 2008, 02:01:52 »

So I tried to implement this using your ModuleTest, but once I moved the module loading into a command object, I lost the ability to know which ModuleLoader to load the module into.  I'm sure you can chalk this up to my lack of experience, but how would you accomplish it?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #13 on: January 22, 2008, 05:33:35 »

You could use the 'type' parameter to the notification to hold the name of the module to load and let the body be the reference to the ModuleLoader to load said module into.

-=Cliff>
Logged
zilet
Courseware Beta
Jr. Member
***
Posts: 17


View Profile WWW Email
« Reply #14 on: January 23, 2008, 10:16:41 »

Hola,

   I refactored some code and created Commands for loading/unloading modules. But while I was testing the solution I found one problem. After the module is unloaded next time it is loaded registered commands get called twice. I am doing unregistration of all commands, proxies and mediators that module had when it unloads.

  Right now I don't see the problem so please help  ???

  The code is attached to this post.
Logged
Pages: [1] 2
Print