accessing a mediator in a command
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!
Comments are currently closed for this discussion. You can start a new one.
Support Staff 2 Posted by Joel Hooks on 08 May, 2010 11:25 PM
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>3 Posted by tom on 08 May, 2010 11:32 PM
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>4 Posted by Stray on 09 May, 2010 10:44 AM
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.
5 Posted by tom on 13 May, 2010 06:57 PM
i've been following joel's advise - it is working fine. this question can be closed in happiness.
6 Posted by nikdudnik on 04 Jun, 2010 03:42 PM
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.
Stray closed this discussion on 12 Feb, 2011 01:14 AM.