Command mapping and unmapping in the mediator

alexandrratush's Avatar

alexandrratush

25 Jun, 2015 09:40 PM

Hello, everyone. I'll provide an example. There are view and mediator; the view has a startTimer button. When pressing on it the StartTimerCommand is executed. Is it possible to map and unmap the command in the mediator? Like this:

// SimpleViewMediator
override public function initialize():void 
{
    eventCommandMap.map(SimpleTimerEvent.START_TIMER, SimpleTimerEvent).toCommand(StartTimerCommand);
    addViewListener(SimpleViewEvent.START_BUTTON_TIMER, startButtonTimerHandler, SimpleViewEvent);
}
override public function destroy():void
{
    eventCommandMap.unmap(SimpleTimerEvent.START_TIMER, SimpleTimerEvent).fromCommand(StartTimerCommand);
}
private function startButtonTimerHandler(e:SimpleViewEvent):void 
{
    dispatch(new SimpleTimerEvent(SimpleTimerEvent.START_TIMER));
}

This discussion was closed! See this FAQ for more information:

  1. How do I Map a Mediator?

    A Mediator can be mapped in any class that has has the mediatorMap instance available. This includes the Mediator, Context, and Command classes.

    This is the syntax for mapping a mediator:

    mediatorMap.mapView( ViewClass, MediatorClass, injectViewAs, autoCreate, autoRemove );
    
    See more..
  1. alexandrratush closed this discussion on 25 Jun, 2015 09:40 PM.

  2. alexandrratush re-opened this discussion on 25 Jun, 2015 09:41 PM

  3. Support Staff 1 Posted by Ondina D.F. on 26 Jun, 2015 07:06 AM

    Ondina D.F.'s Avatar

    Hi Alexandr,

    Sure, you can do that, but it is a bit unusual to use a Mediator for mappings.

    If you really want to do it inside the Mediator, you'll need to inject the IEventCommandMap into the Mediator.

    Ondina

  4. 2 Posted by alexandrratush on 26 Jun, 2015 11:48 AM

    alexandrratush's Avatar

    Hi Ondina.
    Yes, i have injected IEventCommandMap into the Mediator, and it works. But i'd like to know where i can use mapping and unmapping of specific command for this mediator that can be used only inside of it. Only in Config? Or somewhere else? What is the best practice for doing this?

  5. Support Staff 3 Posted by Ondina D.F. on 26 Jun, 2015 12:39 PM

    Ondina D.F.'s Avatar

    Usually, we perform the mappings at startup, like this:

      _context = new Context()
    .install(MVCSBundle)
    .configure(MediatorsConfig, ModelsConfig, ServicesConfig, ControllersConfig)
    .configure(contextView);
    

    MediatorsConfig, ModelsConfig, ServicesConfig, ControllersConfig are classes that implement robotlegs IConfig and have a public function configure(), where you can perform the mappings.
    For example, ControllersConfig is injected with an IEventCommandMap and maps events to commands. MediatorsConfig, injected with an IMediatorMap, maps views to mediators, and so on.

    If for some reason you need to make a mapping at a later time, you can do it in a command.
    Same for un-mapping.

    Say, you've mapped your StartTimerCommand in the ControllersConfig from the above example. When your SimpleViewMediator is destroyed it can dispatch an event to trigger an UnmapTimerCommand, where you do the necessary un-mappings. The advantage of doing it in commands is that they are short lived and get garbage collected after execution and the Mediator is only taking care of relaying events from/to view.

    Mediator:

    override public function destroy():void
    {
        dispatch(new SimpleTimerEvent(SimpleTimerEvent.UNMAP_TIMER));
        super.destroy();
    }
    

    Command:

    [Inject]
    public var commandMap:IEventCommandMap;
    
    public function execute():void
    {
        commandMap.unmap(SimpleTimerEvent.START_TIMER, SimpleTimerEvent).fromAll();
    
    //or 
    //commandMap.unmap(SimpleTimerEvent.START_TIMER, SimpleTimerEvent). fromCommand (StartTimerCommand);
    
    commandMap.unmap(SimpleTimerEvent.UNMAP_TIMER, SimpleTimerEvent).fromAll();
    
    //and so on
    }
    

    Does this answer your question?

  6. 4 Posted by alexandrratush on 26 Jun, 2015 12:46 PM

    alexandrratush's Avatar

    Yes, thank you very much. Your answer was very helpful.

  7. alexandrratush closed this discussion on 26 Jun, 2015 12:46 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac