mattwallace
mattwallace

Reputation: 4222

how do I dynamically map view to mediator in Haxe using Cube framework

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);
    }
}



}

Upvotes: 0

Views: 521

Answers (2)

Tomasz Szych
Tomasz Szych

Reputation: 21

I'm new to haxe and I'm trying to do sth that Matt wrote about. So that's what I've worked out so far:

in initiate() method of your agent:

injector.mapInstance( Main, container ); // lets you inject the "container" into commands
mediatorMap.mapView( Welcome, WelcomeMediator );

then create a dedicated command class (dont do it in a mediator due to architectural reasons) and inject "container":

@Inject
public var container : Main;

in execute() method:

var welcomeView = new Welcome();
container.addChild( welcomeView );
mediatorMap.createMediator( welcomeView );

NOTICE: I found out that you need to modify Agent.hx from Cube framework. Scroll to the bottom of this class and get rid of underscores in the last method. It should look like that:

private function bindMappings()
{
    injector.mapInstance( ICentralDispatcher, eventDispatcher );
    injector.mapSingleton( Injector );
    injector.mapInstance( ICommandMap, commandMap );
    injector.mapInstance( IMediatorMap, mediatorMap );
    injector.mapInstance( IViewMap, viewMap );
    injector.mapInstance( IProxy, proxy );
}

Seems that mapInstance won't work on private properties or sth ;)

Upvotes: 2

nMoncho
nMoncho

Reputation: 370

I don't know the framework, only worked with Robotlegs, but probably the only way you could do it is by mapping the View and the Mediator in a Command execute. You should create an Event that holds the View and Mediator classes, register the Command for that event, dispatching when needed, and on execution, the Command should use mediatorMap.

Hope it helps.

Upvotes: 0

Related Questions