tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/4277-tovalueRobotlegs: Discussion 2013-08-02T16:41:21Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/279854472013-07-27T10:20:53Z2013-07-27T10:20:54ZtoValue()<div><pre>
<code>test path code</code>
</pre></div>Itag:robotlegs.tenderapp.com,2009-10-18:Comment/279854472013-07-27T10:42:26Z2013-07-27T10:42:27ZtoValue()<div><p>in my code has a bug which I forgot to remove<br>
public var num:int = 0;</p></div>Itag:robotlegs.tenderapp.com,2009-10-18:Comment/279854472013-07-28T08:37:18Z2013-07-28T08:37:18ZtoValue()<div><p>First rule: the injector can only inject something ( classes,
properties) into a class, if it "knows" about it, i.e. if these
classes/properties have been already mapped and the class to be
injected into is also known by the Injector.<br>
So, the Injector has to have a mapping for ClassC and also for the
property num.</p>
<pre>
<code>//1
var someNumber:int = 555;
injector.map(int).toValue(someNumber);
//2
injector.map(ClassC).asSingleton();
//3
injector.map(ClassA).asSingleton();
injector.getInstance (ClassA);</code>
</pre>
<p>//1 injector.map(int).toValue(someNumber); This lets the
injector know that whenever it "sees" an [Inject] metatag for a
property of type int, it will have to inject someNumber into that
class.</p>
<p>If you had<br>
[Inject] public var num:int; in your ClassC,</p>
<p>and [Inject] public var anotherNum:int; in another (mapped)
class,<br>
the Injector will inject someNumber with a value of 555 into both
classes.</p>
<p>//2 injector.map(ClassC).asSingleton(); Makes the mapping return
a lazily constructed singleton instance of the mapped type for each
consecutive request</p>
<p>Now, in ClassA you inject ClassC, for which you already have a
mapping. That's good. But if you want to access ClassC's
properties, you'll have to do it like so:</p>
<pre>
<code>public class ClassA
{
[Inject]
public var _c:ClassC;
public function ClassA()
{
//do nothing in here
}
[PostConstruct]
public function someMethod():void
{
trace(_c.num);//traces 555
}
}</code>
</pre>
<p>var _c won't be available in ClassA's constructor !! See this
for more details:<br>
<a href=
"https://github.com/robotlegs/robotlegs-framework/wiki/Common-Problems#wiki-null-in-constructor">
https://github.com/robotlegs/robotlegs-framework/wiki/Common-Proble...</a></p>
<p>Then the other problem is having _c = <strong>new
ClassC();</strong> in the constructor of ClassA.<br>
This _c will be <strong>another instance</strong> of ClassC,
different from the one you injected by using the [Inject] metatag,
and the Injector won't know anything about it, so it won't inject
"num" into it. As said before, the instances need to be constructed
by the Injector.</p>
<p>Does that help?</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/279854472013-07-28T11:19:13Z2013-07-28T11:19:13ZtoValue()<div><p>Ondina D.F. Thank you for your help, I went to continue to
learn.</p></div>Itag:robotlegs.tenderapp.com,2009-10-18:Comment/279854472013-08-02T16:41:20Z2013-08-02T16:41:20ZtoValue()<div><p>No problem. Have a nice time learning robotlegs 2 :)</p></div>Ondina D.F.