I've been working with and learning the following micro framework Cube https://github.com/xirsys/cube for use in my Haxe projects.
The examples I've found have been very helpful but one thing the examples that I've ran across don't have that I'd like to figure out is registering and mapping views to mediators at runtime.
I think I'm close but it just doesn't seem to be working. Heres what my AppContext looks like.
class AppContext extends Agent<Dynamic, IEvent>
{
public function new(container: Dynamic, autoStart:Bool)
{
super(container, autoStart);
}
override public function initiate()
{
mediatorMap.mapView(Main, MainMediator);
mediatorMap.mapView(Welcome, WelcomeMediator);
injector.mapSingleton(AppModel, container);
dispatch(AgentEvent.STARTUP_COMPLETE, null);
}
}
here is what's going on my my Main view
class Main extends Sprite
{
public var agent: AppContext;
@Inject
public var dm:AppModel;
public function new()
{
super();
addEventListener(Event.ADDED_TO_STAGE, onAdded);
haxe.Log.setColor(0xffffff);
this.name = "main view";
}
public static function main(): Void
{
Lib.current.addChild(new Blastroidz());
}
private function onAdded(e:Event):Void
{
agent = new AppContext(this, false);
agent.addEventHandler(AgentEvent.STARTUP_COMPLETE, onContextStart);
agent.initiate();
}
private function onContextStart(evt:IEvent):Void
{
agent.mediatorMap.createMediator(this);
}
}
Now after the main view loads up in my Main view Mediator I create the "welcome" view and then I would like to create it's mediator and use it like so.
class MainMediator extends xirsys.cube.mvcs.Mediator
{
@Inject
public var view:Main;
public var welcome:Welcome;
private var viewManager:NMEViewManager;
override public function onRegister()
{
super.onRegister();
eventDispatcher.addEventHandler(AppEvent.SET_NEW_VIEW, changeViewHandler);
initView();
}
private function initView():Void
{
viewManager = new NMEViewManager(view);
welcome = new Welcome();
viewManager.setView(welcome, NMEViewManager.FADE);
///// this is the important line that doesn't seem to work \\\\\\
mediatorMap.createMediator(welcome);
}
private function changeViewHandler(e:String):Void
{
trace("change view");
}
My welcome mediator looks like so and the onRegister doesn't seem to fire and I don't get any errors at all .... note that viewManager handles addChild to the main view of the welcome view and this seems to work just fine.
class WelcomeMediator extends xirsys.cube.mvcs.Mediator
{
@Inject
public var view:Welcome;
override public function onRegister()
{
super.onRegister();
view.addEventListener(Event.COMPLETE, onCompleteHandler);
trace("welcome mediator registered");
}
private function onCompleteHandler(e:Event):Void
{
trace("logo complete");
var event:AppEvent = new AppEvent(AppEvent.SET_NEW_VIEW);
event.view_name = AppModel.MAIN;
this.eventDispatcher.dispatch(AppEvent.SET_NEW_VIEW, event);
}
}
}