I get a strange behavior when registering a mediator interested in a notification being processed by the
View.notifyObservers() function.
I've a simple application that notify the observers of the application startup.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
creationComplete="handleCreationComplete()"
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import org.puremvc.patterns.observer.Notification;
private function handleCreationComplete() : void {
// notify application startup...
ApplicationFacade.getInstance().notifyObservers(
new Notification(ApplicationFacade.STARTUP, this));
}
]]>
</mx:Script>
</mx:Application>
The ApplicationFacade assign the StartupCommand to the STARTUP notification.
package {
import org.puremvc.patterns.facade.Facade;
import org.puremvc.interfaces.IFacade;
public class ApplicationFacade extends Facade implements IFacade {
public static const STARTUP : String = "STARTUP";
public static function getInstance() : ApplicationFacade {
if (instance == null) { instance = new ApplicationFacade();}
return instance as ApplicationFacade;
}
override protected function initializeController() : void {
super.initializeController();
registerCommand(STARTUP, StartupCommand);
}
}
}
The StartupCommand create & register a mediator...
package {
import org.puremvc.patterns.command.SimpleCommand;
import org.puremvc.interfaces.INotification;
public class StartupCommand extends SimpleCommand {
override public function execute(note : INotification) : void {
// initilize & register a mediator
facade.registerMediator(new TestMediator());
}
}
}
The mediator created is interested in the STARTUP notification...
package {
import org.puremvc.patterns.mediator.Mediator;
import org.puremvc.interfaces.INotification;
public class TestMediator extends Mediator {
override public function listNotificationInterests() : Array {
return [ApplicationFacade.STARTUP];
}
override public function handleNotification(note : INotification) : void {
switch (note.getName()) {
case ApplicationFacade.STARTUP:
trace("Bug !!!");
break;
}
}
override public function getMediatorName() : String {
return "TestMediator";
}
}
}
The "handleNotification" should be called if a new STARTUP notification is lunched... BUT it is called direcly after the mediator creation... (because the for loop in the View.notifyObservers is not yet completed)
That not what the asdoc said :
All previously attached <code>IObservers</code> for this <code>INotification</code>'s list are notified and are passed a reference to the <code>INotification</code> in the order in which they were registered.