Skip to content

Instantly share code, notes, and snippets.

@joshtynjala
Created September 5, 2010 07:32
Show Gist options
  • Save joshtynjala/565832 to your computer and use it in GitHub Desktop.
Save joshtynjala/565832 to your computer and use it in GitHub Desktop.
When a RobotLegs mediator is created for an IWindow in Flex, this MediatorMap extension treats the window as a secondary contextView so that new children of the window automatically get mediators as well.

Usage

Add this simple override to your application’s Context:

override protected function get mediatorMap():IMediatorMap
{
	return _mediatorMap || (_mediatorMap = new MultiWindowFlexMediatorMap(contextView, injector.createChild(), reflector));
}

When you create a new IWindow (Spark or MX), create its mediator manually:

package com.example
{
	import com.example.view.components.ExampleWindow;
	import org.robotlegs.mvcs.Command;
	public class CreateExampleWindowCommand extends Command
	{
		override public function execute():void
		{
			var ExampleWindow = new ExampleWindow();
			this.mediatorMap.createMediator(window);
			window.open();
		}
	}
}

Now the window should be treated just like a contextView by the MediatorMap so that new children automatically get mediators.

package org.robotlegs.base
{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Stage;
import flash.events.Event;
import flash.events.IEventDispatcher;
import mx.core.IWindow;
import mx.events.AIREvent;
import mx.events.FlexEvent;
import org.robotlegs.core.IInjector;
import org.robotlegs.core.IMediator;
import org.robotlegs.core.IReflector;
public class MultiWindowFlexMediatorMap extends MediatorMap
{
public function MultiWindowFlexMediatorMap(contextView:DisplayObjectContainer, injector:IInjector, reflector:IReflector)
{
super(contextView, injector, reflector);
}
private var _windows:Vector.<IWindow>;
override public function set enabled(value:Boolean):void
{
if (value != _enabled)
{
for each(var window:IWindow in this._windows)
{
this.removeWindowListeners(window);
}
super.enabled = value;
for each(window in this._windows)
{
this.addWindowListeners(window);
}
}
}
override protected function activate():void
{
if (!_active)
{
super.activate();
for each(var window:IWindow in this._windows)
{
this.addWindowListeners(window);
}
}
}
override public function createMediator(viewComponent:Object):IMediator
{
if(viewComponent is IWindow && viewComponent != this.contextView)
{
var window:IWindow = IWindow(viewComponent);
if(!this._windows)
{
this._windows = new Vector.<IWindow>;
}
var index:int = this._windows.indexOf(window);
if(index < 0)
{
this.addWindowListeners(window);
this._windows.push(window);
}
}
return super.createMediator(viewComponent);
}
override public function removeMediator(mediator:IMediator):IMediator
{
if(mediator)
{
var viewComponent:Object = mediator.getViewComponent();
if(viewComponent is IWindow)
{
var window:IWindow = IWindow(viewComponent);
var index:int = this._windows.indexOf(window);
if(index >= 0)
{
this._windows.splice(index, 1);
}
this.removeWindowListeners(window);
for(var view:Object in this.mediatorByView)
{
if(view != window && DisplayObjectContainer(window).contains(DisplayObject(view)))
{
this.removeMediatorByView(view);
}
}
}
}
return super.removeMediator(mediator);
}
protected function addWindowListeners(window:IWindow):void
{
if (enabled && _active)
{
if(!window.nativeWindow)
{
IEventDispatcher(window).addEventListener(FlexEvent.PREINITIALIZE, window_preinitializeHandler, false, 0, true);
return;
}
var stage:Stage = window.nativeWindow.stage;
stage.addEventListener(Event.ADDED_TO_STAGE, onViewAdded, useCapture, 0, true);
stage.addEventListener(Event.REMOVED_FROM_STAGE, onViewRemoved, useCapture, 0, true);
}
}
protected function removeWindowListeners(window:IWindow):void
{
if(window.nativeWindow && enabled && _active)
{
var stage:Stage = window.nativeWindow.stage;
stage.removeEventListener(Event.ADDED_TO_STAGE, onViewAdded, useCapture);
stage.removeEventListener(Event.REMOVED_FROM_STAGE, onViewRemoved, useCapture);
}
}
protected function window_preinitializeHandler(event:FlexEvent):void
{
var window:IWindow = IWindow(event.currentTarget);
IEventDispatcher(window).removeEventListener(FlexEvent.PREINITIALIZE, window_preinitializeHandler);
this.addWindowListeners(window);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment