toValue()

I's Avatar

I

27 Jul, 2013 10:18 AM

Followed your advice and read all day yesterday.
today launched a study on small examples.
tell me what am I doing wrong?
`package
{
public class ClassC
{
[Inject]
public var num:int = 0;

public function ClassC()
{
//
}

}

}`
`var myClass:ClassC = injector.instantiateUnmapped(ClassC);
myClass.num = 555;
injector.map(ClassC).toValue(myClass);
injector.injectInto(myClass);
injector.map(ClassA).asSingleton();
super.injector.instantiateUnmapped(ClassA);`
`package
{
public class ClassA
{
[Inject]
public var _c:ClassC;

public function ClassA()
{
_c = new ClassC();
trace(_c.num);//why the value is not 555?
}

}

}`

  1. I closed this discussion on 27 Jul, 2013 10:18 AM.

  2. I re-opened this discussion on 27 Jul, 2013 10:20 AM

  3. 1 Posted by I on 27 Jul, 2013 10:20 AM

    I's Avatar

    @@@
    test path code
    @@@

  4. 2 Posted by I on 27 Jul, 2013 10:42 AM

    I's Avatar

    in my code has a bug which I forgot to remove
    public var num:int = 0;

  5. Support Staff 3 Posted by Ondina D.F. on 28 Jul, 2013 08:37 AM

    Ondina D.F.'s Avatar

    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.
    So, the Injector has to have a mapping for ClassC and also for the property num.

    //1
    var someNumber:int = 555;
    injector.map(int).toValue(someNumber);
    
    //2
    injector.map(ClassC).asSingleton();
    
    //3
    injector.map(ClassA).asSingleton(); 
    injector.getInstance (ClassA);
    

    //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.

    If you had
    [Inject] public var num:int; in your ClassC,

    and [Inject] public var anotherNum:int; in another (mapped) class,
    the Injector will inject someNumber with a value of 555 into both classes.

    //2 injector.map(ClassC).asSingleton(); Makes the mapping return a lazily constructed singleton instance of the mapped type for each consecutive request

    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:

    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
        }
    }
    

    var _c won't be available in ClassA's constructor !! See this for more details:
    https://github.com/robotlegs/robotlegs-framework/wiki/Common-Proble...

    Then the other problem is having _c = new ClassC(); in the constructor of ClassA.
    This _c will be another instance 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.

    Does that help?

  6. 4 Posted by I on 28 Jul, 2013 11:19 AM

    I's Avatar

    Ondina D.F. Thank you for your help, I went to continue to learn.

  7. Support Staff 5 Posted by Ondina D.F. on 02 Aug, 2013 04:41 PM

    Ondina D.F.'s Avatar

    No problem. Have a nice time learning robotlegs 2 :)

  8. Ondina D.F. closed this discussion on 02 Aug, 2013 04:41 PM.

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

Keyboard shortcuts

Generic

? 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