I have a view object! How should I mediate it? (answer is:Mediator)

First of all you need to create mediator class.

Mediators are classes that aggregate(hides) your view objects, and acts as middle man for sending messages in and out. (provides indirect communication)

To create mediator you need:

  • extend Mediator class.
  • override onRegister and onRemove functions.
  • Add public variable typed to view objects class and with [Inject] tag.
[as3 gutter=”1″ highlight=”4,6,7,9,14″] package org.mvcExpress.sampleProject.view.myTest{
import org.mvcexpress.mvc.Mediator;

public class MyViewMediator extends Mediator {

[Inject] public var view:MyView;

override public function onRegister():void {
trace("MyViewMediator.onRegister called! view:" + view );
// start mediating view
}

override public function onRemove():void {
//dispose of mediator, remove event listeners.
}

}
}[/as3]


Then mediator class is created – next step is to pair it with view objects class by using mediatorMap.map().
mediatorMap is accessible from your MainModule objects onInit() function.

[as3 gutter=”1″ highlight=”12″] package org.mvcExpress.sampleProject{
import org.mvcexpress.core.ModuleCore;
import org.mvcExpress.sampleProject.view.main.MainMediator;
import org.mvcExpress.sampleProject.view.myTest.MyView;
import org.mvcExpress.sampleProject.view.myTest.MyViewMediator;

public class MainModule extends ModuleCore {

override protected function onInit():void {
// set up application
mediatorMap.map(Main, MainMediator);
mediatorMap.map(MyView, MyViewMediator);
}

public function start(main:Main):void {
// mediate main view.
mediatorMap.mediate(main);
}

}
}[/as3]

(This work also can be moved to command.)


Last step is mediating you view object by using mediatorMap.mediate();.

The best place for this job is parent-mediator – mediator that mediates the parent view.
Lets say MainMediator wants to add instance of MyView to view it is mediating.

MainMediator will then do only adding(removing) and mediating(unmediating) work for our instance of MyView. State of myView object have to be handled not by MainMediator, thats work of MyViewMediator.

Then you mediatorMap.mediate(); your view object – mediator that paired with this object is automatically created, and view object is injected into it.

[as3 gutter=”1″ highlight=”14,15,16,20,21″] package org.mvcExpress.sampleProject.view.main{
import org.mvcExpress.sampleProject.Main;
import org.mvcexpress.mvc.Mediator;
import org.mvcExpress.sampleProject.view.myTest.MyView;

public class MainMediator extends Mediator {

[Inject] public var view:Main;

var myView:MyView;

override public function onRegister():void {
myView = new MyView();
view.addChild(myView);
mediatorMap.mediate(myView);
}

override public function onRemove():void {
view.removeChild(myView);
mediatorMap.unmediate(myView);
}

}
}
}[/as3]

(There are couple of strategies how to add and mediate your view object by parent-mediator, or commands. But that is another question…)


  • It is a good practice to end your mediator class names with ‘Mediator’.
  • It is a good practice to put your mediator’s in package ‘view’. (organized to sub-folders by application features or by display object tree.)
  • Mediators can be instantiated only by framework.