How to access the main Context Class...

tehPrinny's Avatar


13 Aug, 2011 01:19 AM

I've only been using Robotlegs for a few hours, so I apologise in advance if I am missing something obvious...

Is there any way to access the main Context Class (where I map my mediators, commands, etc.) from my mediators, models, etc.?

If not, how would one go about triggering the mediatorMap.unmapView() or commandMap.unmapEvent() at a later point in the application?

Thanks in advance...

  1. 1 Posted by Michal Wroblews... on 13 Aug, 2011 01:40 AM

    Michal Wroblewski's Avatar


    You can easily [Inject] all of these like this:

    public var mediatorMap:IMediatorMap;

    BTW if you want to use it in command these are already injected:

    public var contextView:flash.display.DisplayObjectContainer;
    public var commandMap:org.robotlegs.core.ICommandMap;
    public var;
    public var injector:org.robotlegs.core.IInjector;
    public var mediatorMap:org.robotlegs.core.IMediatorMap;


  2. 2 Posted by neil on 13 Aug, 2011 07:57 AM

    neil's Avatar

    as an addendum to michal's post.

    a command in RobotLegs is anything that has a public execute():void method.
    to get the pre injected utils above, you'll have to make sure that your command extends the Command class.


  3. 3 Posted by tehPrinny on 13 Aug, 2011 09:27 AM

    tehPrinny's Avatar

    Thanks for the replies - You've answered some additional questions.

    However, I still need some clarification around the following : How would I trigger a method in my Context class from say, for example, a Command class?

    e.g. My main Context is called GameContext, which extends Context. How would I trigger the GameContext.shutdown() method from the Command?

    Thanks again for your assistance thus far. Very much appreciated.

  4. 4 Posted by neil on 13 Aug, 2011 10:06 AM

    neil's Avatar

    the simple answer is you wouldn't call a method on your context
    your Main View ( I assume GameView in this case) has knowledge of the context.
    Mediate your MainView, and have it listen for a SHUT DOWN event.
    then it can call a shutdown method on your GameView, and that in turn can call shut down on your Context.

    I'm sure there are other ways too....


  5. 5 Posted by tehPrinny on 13 Aug, 2011 11:35 AM

    tehPrinny's Avatar

    Apologies, I just checked my post and realised I made an error, which I have corrected :
    GameView.shutdown() was meant to be GameContext.shutdown()

    However, I understand what you are saying.

    Last question (for now ;), how would one call the shutdown() in the Context?

  6. Support Staff 6 Posted by creynders on 13 Aug, 2011 01:23 PM

    creynders's Avatar

    as Neil said, mediate your GameView, have it listen for ContextEvent.SHUT_DOWN, and let it call GameContext#shutdown

  7. 7 Posted by tehPrinny on 13 Aug, 2011 01:50 PM

    tehPrinny's Avatar

    Thanks creynders, I believe this is what I have been doing, but seem to be running into issues...

    Could you take a look at the sample code attached and let me your thoughts?

  8. Support Staff 8 Posted by creynders on 13 Aug, 2011 05:23 PM

    creynders's Avatar

    OK, there are a few things going wrong.

    Both Neil and I thought GameView is your contextview, but apparently it's not.

    The topmost view, the one that instantiates your context is the contextview, it passes itself to the context's constructor function. You need to mediate that view. For sake of example let's call it FooApplication. Normally it also stores the context in a member (or it should anyway)

    You need to create a mediator for it, let's call it ContextViewMediator.

    public var view : FooApplication
    override public function onRegister():void{
        this.addContextListener( ContextEvent.SHUT_DOWN, handle_ShutDownEvent );
    private function handle_ShutDownEvent( event : ContextEvent ) : void{
        view.context.shutdown(); //'context' being the name of the member you stored the context in

    Also "GameContext.shutdown" means you're trying to call a static method, not an instance method, that's why it doesn't work, since there's no static method in GameContext called "shutdown"

  9. Support Staff 9 Posted by Ondina D.F. on 02 Nov, 2011 05:45 PM

    Ondina D.F.'s Avatar

    Thank you for posting.
    Feel free to reopen this discussion if you feel the answer(s) to be dis-satisfactory or if you need more help with this issue, otherwise we'll consider the thread closed. Please open new threads for new issues.

  10. Ondina D.F. closed this discussion on 02 Nov, 2011 05:45 PM.

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

Keyboard shortcuts


? 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