tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/363-n00b-injection-errorRobotlegs: Discussion 2013-04-28T10:24:36Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/40040782010-11-27T21:05:16Z2010-11-27T21:05:16Z[n00b] Injection Error<div><p>Views aren't automatically available for injection - if you
really want to inject the view then you'll need to use mapValue
like this in your context:</p>
<p>var inventoryPopup:InventoryPopup = new InventoryPopup();<br>
injector.mapValue(InventoryPopup, inventoryPopup);</p>
<p>Then the same view will get injected into your command each
time...</p>
<p>But...</p>
<p>Usually view stuff should be handled through the view itself and
triggered through events to the mediator.</p>
<p>So - normally in this situation you'd mediate the targetView and
have the mediator run an api function such as</p>
<pre>
<code>.addInventoryView();</code>
</pre>
<p>Commands are for application logic - models, services etc.</p>
<p>Have a look at some of the examples for how people usually add
new views to their apps.</p>
<p>Hope that helps,</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/40040782010-11-27T21:08:50Z2010-11-27T21:08:51Z[n00b] Injection Error<div><p>Thanks Stray!<br>
That makes much more sense. I figured it had to do with the way I
was trying to do it in the command. I'll let you know if I hit
anymore problems :P</p>
<p>Thanks again,<br>
TJ</p></div>TJ Gillistag:robotlegs.tenderapp.com,2009-10-18:Comment/40040782010-11-27T21:17:54Z2010-11-27T21:19:14Z[n00b] Injection Error<div><p>Howdy TJ,</p>
<p>Commands can not (easily) access views. Mapping a view class to
a mediator class (<code>mediatorMap.mapView(ViewClass,
MediatorClass);</code>) roughly translates to: "when an instance of
ViewClass lands on stage, create an instance of MediatorClass and
inject that view instance into it". The view instance is only
temporarily stored in the injector (the mapping exists only while
the mediator is being created).</p>
<p>Also, there is another problem here: many instances of a
ViewClass may exist, so which instance are you expecting to be
provided by the injector? Let me illustrate:</p>
<pre>
// During configuration:
mediatorMap.mapView(ViewClass, MediatorClass);
// During runtime:
contextView.addChild(new ViewClass());
contextView.addChild(new ViewClass());
contextView.addChild(new ViewClass());
// And then later, in a command:
[Inject] public var view:ViewClass;
</pre>
<p>There is no way to know which instance you want injected. And,
as I mentioned, the MediatorMap only temporarily maps the ViewClass
instance into the injector while creating the mediator for it.</p>
<p>Aside from the technicalities, it is not a good idea to directly
reference view components from your application (Commands, Models
or Services). The only thing that should know about a view instance
is its mediator. Instead of trying to manipulate the view from a
command, dispatch an event from the command that the view's
mediator can respond to.</p>
<p>Hope that helps!</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/40040782010-11-27T21:49:17Z2010-11-27T21:49:18Z[n00b] Injection Error<div><p>Hi Shaun!<br>
I did want you said about dispatching an event and it worked just
peachy :) And thanks for the explanation. That makes total sense
and clarifies some things for you. I love you guy!</p>
<p>TJ</p></div>TJ Gillistag:robotlegs.tenderapp.com,2009-10-18:Comment/40040782010-11-27T22:49:48Z2010-11-27T22:49:48Z[n00b] Injection Error<div><p>No problemo. Glad to hear you're back on track. Cheers,</p></div>Shaun Smith