How should I handle my root DisplayObject? (answer is:Mediator)

First of all you need to create mediator class that will be mediating main display object of your application.

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

To do that you need:

  • extend Mediator class.
  • override onRegister and onRemove functions.
[as3 gutter=”1″ highlight=”4,6,9″] package org.mvcExpress.sampleProject.view.main{
import org.mvcexpress.mvc.Mediator;

public class MainMediator extends Mediator {

override public function onRegister():void {
}

override public function onRemove():void {
}

}
}[/as3]

But job is not finished yet… in fact – mediator without the view object have no reason to exist!
Every mediator must have declared public variable typed to view objects class and with [Inject] tag. (Tag is used to inject view into mediator automatically.)

[as3 gutter=”1″ highlight=”7,8″] package org.mvcExpress.sampleProject.view.main{
import org.mvcExpress.sampleProject.Main;
import org.mvcexpress.mvc.Mediator;

public class MainMediator extends Mediator {

[Inject] public var view:Main;

override public function onRegister():void {
trace("MainMediator.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=”9″] package org.mvcExpress.sampleProject{
import org.mvcexpress.core.ModuleCore;
import org.mvcExpress.sampleProject.view.main.MainMediator;

public class MainModule extends ModuleCore {

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

}
}[/as3]

(This work also can be moved to command.)

Last step is mediating root view object… but first you need to send it to framework.

The best way to do it is to create your own function start() and send root display object as parameter:

[as3 gutter=”1″ highlight=”8″] package org.mvcExpress.sampleProject{
import flash.display.Sprite;

public class Main extends Sprite {

public function Main() {
var module:MainModule = new MainModule();
module.start(this);
}
}
}[/as3]

The last step is to initiate mediating of view object. This is done using mediatorMap.mediate(); by passing view object to it. Framework will automatically find mediator that mediates this view object and construct it.

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

public class MainModule extends ModuleCore {

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

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

}
}[/as3]

  • 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.