tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/634-how-to-access-the-main-context-classRobotlegs: Discussion 2011-11-02T17:45:48Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/92737782011-08-13T01:40:31Z2011-08-13T01:41:00ZHow to access the main Context Class...<div><p>Hi,</p>
<p>You can easily [Inject] all of these like this:</p>
<pre>
<code>[Inject]
public var mediatorMap:IMediatorMap;</code>
</pre>
<p>BTW if you want to use it in command these are already
injected:</p>
<pre>
<code>[Inject]
public var contextView:flash.display.DisplayObjectContainer;
[Inject]
public var commandMap:org.robotlegs.core.ICommandMap;
[Inject]
public var eventDispatcher:flash.events.IEventDispatcher;
[Inject]
public var injector:org.robotlegs.core.IInjector;
[Inject]
public var mediatorMap:org.robotlegs.core.IMediatorMap;</code>
</pre>
<p>Best,<br>
Mike</p></div>Michal Wroblewskitag:robotlegs.tenderapp.com,2009-10-18:Comment/92737782011-08-13T07:57:34Z2011-08-13T07:57:34ZHow to access the main Context Class...<div><p>as an addendum to michal's post.</p>
<p>a command in RobotLegs is anything that has a public
execute():void method.<br>
to get the pre injected utils above, you'll have to make sure that
your command extends the Command class.</p>
<p>cheers</p></div>neiltag:robotlegs.tenderapp.com,2009-10-18:Comment/92737782011-08-13T09:27:29Z2011-08-13T11:31:26ZHow to access the main Context Class...<div><p>Thanks for the replies - You've answered some additional
questions.</p>
<p>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?</p>
<p>e.g. My main Context is called <code>GameContext</code>, which
extends <code>Context</code>. How would I trigger the
<code>GameContext.shutdown()</code> method from the Command?</p>
<p>Thanks again for your assistance thus far. Very much
appreciated.</p></div>tehPrinnytag:robotlegs.tenderapp.com,2009-10-18:Comment/92737782011-08-13T10:06:32Z2011-08-13T10:06:32ZHow to access the main Context Class...<div><p>the simple answer is you wouldn't call a method on your
context<br>
your Main View ( I assume GameView in this case) has knowledge of
the context.<br>
Mediate your MainView, and have it listen for a SHUT DOWN
event.<br>
then it can call a shutdown method on your GameView, and that in
turn can call shut down on your Context.</p>
<p>I'm sure there are other ways too....</p>
<p>Michal?</p></div>neiltag:robotlegs.tenderapp.com,2009-10-18:Comment/92737782011-08-13T11:35:41Z2011-08-13T11:36:25ZHow to access the main Context Class...<div><p>Apologies, I just checked my post and realised I made an error,
which I have corrected :<br>
<code>GameView.shutdown()</code> was meant to be
<code>GameContext.shutdown()</code></p>
<p>However, I understand what you are saying.</p>
<p>Last question (for now ;), how would one call the
<code>shutdown()</code> in the Context?</p></div>tehPrinnytag:robotlegs.tenderapp.com,2009-10-18:Comment/92737782011-08-13T13:23:59Z2011-08-13T13:23:59ZHow to access the main Context Class...<div><p>as Neil said, mediate your GameView, have it listen for
ContextEvent.SHUT_DOWN, and let it call GameContext#shutdown</p></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/92737782011-08-13T13:50:51Z2011-08-13T13:50:51ZHow to access the main Context Class...<div><p>Thanks creynders, I believe this is what I have been doing, but
seem to be running into issues...</p>
<p>Could you take a look at the sample code attached and let me
your thoughts?</p></div>tehPrinnytag:robotlegs.tenderapp.com,2009-10-18:Comment/92737782011-08-13T17:23:10Z2011-08-13T17:23:10ZHow to access the main Context Class...<div><p>OK, there are a few things going wrong.</p>
<p>Both Neil and I thought GameView is your contextview, but
apparently it's not.</p>
<p>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)</p>
<p>You need to create a mediator for it, let's call it
ContextViewMediator.</p>
<pre>
<code>//ContextViewMediator
[Inject]
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
}</code>
</pre>
<p>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"</p></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/92737782011-11-02T17:45:46Z2011-11-02T17:45:46ZHow to access the main Context Class...<div><p>Thank you for posting.<br>
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.<br>
Ondina</p></div>Ondina D.F.