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]
Print
Author Topic: Loading Module Multiple Times Error  (Read 10897 times)
danmvc
Jr. Member
**
Posts: 13


View Profile Email
« on: July 10, 2009, 09:45:25 »

Hi, I have a module that represents a gui 'window'. Each time I need a new window I was thinking of loading a new window module and adding it to some container. However, I have hit an error. The first module load is fine, however on subsequent module loads of the same file, the getInstance method returns a null value error. I'm guessing because the NAME of the module has already been registered in the instanceMap even though it is actually a different module.

Is there a way around this? Or is the best practice not to load multiple instances of the same module. Maybe I should rewrite the module so instead of being used for just one window instance, it handles ALL window instances of that type. That way when I need a new window I just tell that module to generate a new window view internally and not load a whole new module .swf.

Thanks for any tips,

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



View Profile WWW Email
« Reply #1 on: July 10, 2009, 09:55:20 »

When you get the facade instance for the module, use a unique name for the module rather than just the NAME constant.

In MyModule, try something similar to this:
:
public static var moduleCount:Number = 0;
public var facade:MyModuleFacade = MyModuleFacade.getInstance( MyModuleFacade.NAME+"/"+moduleCount++ );

-=Cliff>
Logged
danmvc
Jr. Member
**
Posts: 13


View Profile Email
« Reply #2 on: July 10, 2009, 10:21:58 »

Hmm tried it and it's still giving me the error. I guess since it's two separate SWFs using the same class, they are actually treated as separate classes. Changing the static variable moduleCount applies to the first module, but then when the second module loads it starts over since it's a new class. Maybe there is a problem with my getInstance function, I read in one of the many tutorials I've gone through to write it as such:

:
public static function getInstance( key:String ) : ApplicationFacade
 {
     trace("getInstance " + key );
            if ( instanceMap[ key ] == null ) instanceMap[ key ] = new ApplicationFacade( key );
            return instanceMap[ key ] as ApplicationFacade;
     }

But maybe that's not right.

Oh I should probably add that I'm using AS3 (no flex modules) so I'm just extending Sprite class for my module and the facade just extends Facade and implements IFacade.

I've also tried changing the code to:
:
return ApplicationFacade( instanceMap[ key ] );
which gives the following error:

The error I get is:

Type Coercion failed: cannot convert com.scisci.mvc.modules.inspector::ApplicationFacade@3b94de81 to com.scisci.mvc.modules.inspector.ApplicationFacade.


« Last Edit: July 10, 2009, 10:50:16 by danmvc » Logged
danmvc
Jr. Member
**
Posts: 13


View Profile Email
« Reply #3 on: July 10, 2009, 11:36:32 »

I can get around the problem by adding getTimer() to the initialization name/key. But that seems hacky, also I don't know if that will cause problems later.

I know I must be doing something wrong elsewhere because other people are obviously getting this to work loading the same module multiple times. It seems like the two modules should have different multiton keys no?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #4 on: July 11, 2009, 07:52:51 »

Why are you loading 2 swfs that are the same module with the same facade? This is the part that isn't making any sense to me.

-=Cliff>
Logged
danmvc
Jr. Member
**
Posts: 13


View Profile Email
« Reply #5 on: July 11, 2009, 07:03:58 »

Sorry for not being clear.

The way I understood modules is that I could have a module, let's say WindowModule (it makes a user interface window on the screen).

Every time I want to instantiate a new window I would load a new WindowModule. Is this not correct?


I have considered an alternative that might be the correct way where a Module is only loaded once and is used more as a Factory. For instance, you would have 1 WindowFactory per application, and therefore wouldn't need to load the Module twice.

 I guess if you could let me know which one is the preferred use of MultiCore, because both will work.
« Last Edit: July 11, 2009, 07:56:03 by danmvc » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #6 on: July 13, 2009, 07:30:30 »

Once loaded you can just create instances of the module.

-=Cliff>
Logged
danmvc
Jr. Member
**
Posts: 13


View Profile Email
« Reply #7 on: July 13, 2009, 12:52:53 »

That's great! Sorry for being a bit dim-witted, how do you create an instance of a module? I'm using pure AS3.

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



View Profile WWW Email
« Reply #8 on: July 13, 2009, 02:04:06 »

How are you loading the swf now?

-=Cliff>
Logged
danmvc
Jr. Member
**
Posts: 13


View Profile Email
« Reply #9 on: July 14, 2009, 01:09:19 »

I use a Loader object to load the .swf, then access the module using the content property of the loader.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #10 on: July 15, 2009, 09:40:11 »

There is no way to 'clone' a  DisplayObject, but whatever the top level class of your swf is, you should be able to construct a new instance of once it's been loaded.

You could add a factory method to it that would return a new instance. Make the instance match an interface of your choosing, so you don't have to know the actual class in the shell.

In the loading app:
:
var myModule:IMyModuleInterface = loader.content as IMyModuleInterface;
var myNewInstance = myModule.getInstance();

In MyModule
:
public class MyModule extends DisplayObject implements IModuleInterface

...

public function getInstance( name:String ):IModuleInterface
{
    var m:IModuleInterface = new MyModule();
    m.setName( name );
    return new MyModule();
}

In IMyModuleInterface:
:
function getInstance():IModuleInterface
function setName():void

-=Cliff>
Logged
danmvc
Jr. Member
**
Posts: 13


View Profile Email
« Reply #11 on: July 15, 2009, 12:42:31 »

Cool thanks I"ll try this out. And I really appreciate you fielding all these questions.
Logged
Pages: [1]
Print