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: Why received this Mediator the STARTUP Event?  (Read 6523 times)
Oscar
Jr. Member
**
Posts: 12


View Profile Email
« on: April 23, 2008, 02:36:37 »

Hi,

just a generally question.
Let's say we have this simple Application:

ApplicationFacade Class:
:

//...............

public function startup(app:App):void
{
       sendNotification(STARTUP, app);
}
 
// Register Commands with the Controller
override protected function initializeController():void
{
       super.initializeController(); 
       registerCommand(STARTUP, StartupCommand); 
}

//..................

StartupCommand Class:
:


//.........

override public function execute(notification:INotification):void
{
var app:App = notification.getBody() as App;
var mediatorName:String = AppMediator.NAME;

facade.registerMediator(new AppMediator(mediatorName, app));
}

//.......


AppMediator Class:
:

//.......
override public function handleNotification(note:INotification):void
{
switch(note.getName())
{
case ApplicationFacade.STARTUP:
    trace("AppMediator recivied the STARTUP EVENT");
    break;
}
}

override public function listNotificationInterests():Array
{
return [
    ApplicationFacade.STARTUP
];
}

//.......



App Class
:

//........

public function App()
{
    var facade:ApplicationFacade = ApplicationFacade.getInstance();
    facade.startup(this);
}

//.........


Like you can see, the AppMediator listening for the STARTUP Event.
When you start this Application he also received this Event. Exactly this makes me curios.
Let's take a look at the start order:

1.) "App" initializes the ApplicationFacade and call the startup function.
2.) The ApplicationFacade sends a STARTUP Event.
3.) Because the Controller listens for this Event, he create/start the "StartupCommand".
4.) This Command create the "AppMediator" ...

At Point 2 the STARTUP Event is throwing. At this Point the "AppMediator" doesn't exist.
Only now at Point 4, the Command creates the Mediator. 

So, why the "AppMediator" actually received the STARTUP Event?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: April 23, 2008, 07:46:28 »

The Command was the first and only observer for the STARTUP notification. However, before it finished executing, it created and registered the Mediator.

During registration of the Mediator, it is interrogated as to its notification interests, and you have said it is interested in the STARTUP notification, therefore it is placed on the observer list for the STARTUP notification.

Then the Mediator is registered and the Command is complete, and the loop over the STARTUP notification observer list continues. The Mediator is now in this list and so it is notified.

-=Cliff>
Logged
Pages: [1]
Print