accessing a mediator in a command

tom's Avatar

tom

08 May, 2010 11:17 PM

i wonder if it is basically a good idea to access a mediator from a command.
what i want to achieve is to add a child to a ViewStack inside the view by calling a method in its mediator.
confusion starts as i have to map more than what is feeling right:

mediatorMap.mapView(ChatView, ChatViewMediator);
injector.mapSingleton(ChatViewMediator);
injector.mapSingleton(ChatView);

the command looks like this:

public class ContactSelectedCommand extends SignalCommand
{
    [Inject]
    public var contactItem : ContactItem;

    [Inject]
    public var chatSessionProxy : ChatSessionProxy;

    [Inject]
    public var chatViewMediator : ChatViewMediator;

    override public function execute() : void {
        var session : ChatSession = chatSessionProxy.getSession(contactItem.rosterItem.jid.toBareJIDString());
        chatViewMediator.addStack(session);
    }
}

the mediator has this method declared:

    public function addStack(session : ChatSession) : void {
        var chatSessionView = new ChatSessionView();
        chatSessionView.session = session;
        view.stack.addChild(chatSessionView);
    }

alternatively, i thought about using the presentation model pattern to retrieve the session inside ChatSessionView. using flex 4 children of a sparked ViewStack need to implement NavigatorContent - but for some reason this component type has no data property i could use to pass a id in to retrieve the according ChatSession from the presentation model. declaring a property to achieve this does not feel right, too.

any best practice to apply? thanks in advance!

  1. Support Staff 1 Posted by Joel Hooks on 08 May, 2010 11:25 PM

    Joel Hooks's Avatar

    They are intentionally difficult to get at in a command. What you want to do is kick an event/signal with the ChatSession from the command that is received by the mediator and the view updated accordingly.

    From the look of it:

    contactItem.rosterItem.jid.toBareJIDString()

    what you REALLY want here is the jid. I'd consider triggering the command with the payload you need and avoid digging that deep into an object </unsolicitatedAdvice>

  2. 2 Posted by tom on 08 May, 2010 11:32 PM

    tom's Avatar

    joel, thank you for your quick reply. i'm going to get some sleep now and try to simplify things tomorrow according to your advice.
    </communitySupportRocks>

  3. 3 Posted by Stray on 09 May, 2010 10:44 AM

    Stray's Avatar

    Agree with Joel.

    And - as a rule - if it's hard to do in RobotLegs then I've probably got a problem with my design.

  4. 4 Posted by tom on 13 May, 2010 06:57 PM

    tom's Avatar

    i've been following joel's advise - it is working fine. this question can be closed in happiness.

  5. 5 Posted by nikdudnik on 04 Jun, 2010 03:42 PM

    nikdudnik's Avatar

    Hey guys! Have got the same question and found the answer here.
    That's ok but the best practices state that Commands may "Be injected with Models, Services, and Mediators to perform work on directly". Should be changed.

  6. Stray closed this discussion on 12 Feb, 2011 01:14 AM.

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