tag:robotlegs.tenderapp.com,2009-10-18:/discussions/solutions/37-access-stage-from-model-hackRobotlegs: Discussion 2012-09-03T08:57:44Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/184388462012-09-01T01:41:05Z2012-09-01T01:41:07ZAccess stage from Model: Hack<div><p>Here's a little hack to allow stage reference from within
classes that don't normally have a reference to the stage (e.g.,
Models). It works by enabling injection of the stage into any RL
class.</p>
<p>Yes, there are better (practice) ways to do this kind of thing,
but I wanted to share the hack anyway.</p>
<p>Let's say you want to notify the app of a change to the device
orientation:</p>
<p>// in the Context injector.mapValue( Stage, contextView.stage
);</p>
<p>// in a Model [Inject] var stage : Stage;<br>
... // event handlers for these listeners would trigger appropriate
Commands to notify the rest of the app about the events.
stage.addEventListener( Stage.onOrientationChange,
onOrientationChange );<br>
stage.addEventListener( Event.MOUSE_LEAVE, onMouseLeave );</p>
<p>... or, maybe you want to reference Stage properties from within
a Command? Just map it and inject it!</p>
<p>[Inject] var stage : Stage;<br>
... override public function execute() : void<br>
{</p>
<pre>
<code>trace( "WTF, stage property reference from a Command?!:", stage.fullScreenWidth );</code>
</pre>
<p>}</p></div>elliot