Over 10 years of community discussion and knowledge are maintained here as a read-only archive.
public class CreateTestCommand extends SimpleCommand implements ICommand { override public function execute (notification:INotification) : void { trace('CreateTestCommand'); var spr:Sprite = new Sprite(); var test:TestMediator = new TestMediator(spr); facade.registerMediator(test); sendNotification(ApplicationFacade.POKE_TEST); sendNotification(ApplicationFacade.REMOVE_TEST); sendNotification(ApplicationFacade.POKE_TEST); var test2:TestMediator = new TestMediator(spr); facade.registerMediator(test2); sendNotification(ApplicationFacade.POKE_TEST); sendNotification(ApplicationFacade.REMOVE_TEST); sendNotification(ApplicationFacade.POKE_TEST); sendNotification(ApplicationFacade.REMOVE_TEST); } }
/** * Tests registering a Mediator for 3 different notifications, removing the * Mediator from the View, and seeing that neither notification causes the * Mediator to be notified. Added for the fix deployed in version 1.7 */ public function testRemoveMediatorAndSubsequentNotify():void { // Get the Singleton View instance var view:IView = View.getInstance(); // Create and register the test mediator to be removed. view.registerMediator( new ViewTestMediator2( this ) ); // Create and register the Mediator to remain view.registerMediator( new ViewTestMediator( this ) ); // test that notifications work view.notifyObservers( new Notification(NOTE1) ); assertTrue( "Expecting lastNotification == NOTE1", lastNotification == NOTE1); view.notifyObservers( new Notification(NOTE2) ); assertTrue( "Expecting lastNotification == NOTE2", lastNotification == NOTE2); view.notifyObservers( new Notification(NOTE3) ); assertTrue( "Expecting lastNotification == NOTE3", lastNotification == NOTE3); // Remove the Mediator view.removeMediator( ViewTestMediator2.NAME ); // test that retrieving it now returns null assertTrue( "Expecting view.retrieveMediator( ViewTestMediator2.NAME ) == null", view.retrieveMediator( ViewTestMediator2.NAME ) == null ); // test that notifications no longer work // (ViewTestMediator2 is the one that sets lastNotification // on this component, and ViewTestMediator) lastNotification = null; view.notifyObservers( new Notification(NOTE1) ); assertTrue( "Expecting lastNotification != NOTE1", lastNotification != NOTE1); view.notifyObservers( new Notification(NOTE2) ); assertTrue( "Expecting lastNotification != NOTE2", lastNotification != NOTE2); view.notifyObservers( new Notification(NOTE3) ); assertTrue( "Expecting lastNotification != NOTE3", lastNotification != NOTE3); cleanup(); }
if ( observers.length == 0 ) { delete observerMap[ notificationName ]; break; }