injector.mapSingleton(Model); but [inject] public var board:Model; returns a null.

davidtynnhammar's Avatar

davidtynnhammar

09 Aug, 2010 06:16 PM

Hello.

In my main context I have a injector.mapSingleton(Model);. Then when I try to access the model in a command by doing [inject] public var model:Model; all I get when I try to access this model is a null.

The model is extending from Actor and the command obviously extends Command.

I get no error messages except for the exception when trying to access that null objects methods.

What have I missed?

  1. Support Staff 1 Posted by Joel Hooks on 09 Aug, 2010 06:34 PM

    Joel Hooks's Avatar

    It is hard to tell what you missed without more context ;)

    Can you paste in the actual code (the command and context) and specify where in the command it is null.

  2. Support Staff 2 Posted by Shaun Smith on 09 Aug, 2010 06:40 PM

    Shaun Smith's Avatar

    Perhaps it is null in your command's constructor? Dependencies injected via setter/property injection are not available until after the instance has been created. Have a look at: http://wiki.github.com/robotlegs/robotlegs-framework/common-problems (under "Problem: Injected Properties Are Null In Constructor").

  3. 3 Posted by davidtynnhammar on 09 Aug, 2010 06:55 PM

    davidtynnhammar's Avatar

    ` public class MyContext extends Context {

    public function MyContext(contextView:DisplayObjectContainer) {     
        super(contextView);
    }
    
    override public function startup():void {
    
            injector.mapSingleton(Model);
    
        commandMap.mapEvent(MEvent.TRY_MOVE, ACommand, MEvent);
    
        super.startup();
    }

    }`

    ` public class ACommand extends Command {

    [inject]
    public var m:Model;
    
    public function ACommand(e:MEvent) {
        trace(m);
    
    }

    } `

  4. 4 Posted by davidtynnhammar on 09 Aug, 2010 06:57 PM

    davidtynnhammar's Avatar

    Err.. Seems like I can't make it format correctly, but I hope you can understand it anyway :). (Thanks for the quick replies!)

  5. Support Staff 5 Posted by Joel Hooks on 09 Aug, 2010 07:00 PM

    Joel Hooks's Avatar

    The property injection isn't available yet. It will be available for the execute() method of the command. To get the injection in the constructor you could:

    public function ACommand(e:MEvent, m:Model) {

     this.m = m;
     trace(m);

    }

  6. 6 Posted by Jason Dias on 09 Aug, 2010 07:01 PM

    Jason Dias's Avatar

    Your command should overwrite execute, if for some reason you need the model
    available in the constructor of the command you need to use [PostContruct]
    tag as Shaun described here
    http://wiki.github.com/robotlegs/robotlegs-framework/common-problems (under
    "Problem: Injected Properties Are Null In Constructor").

    On Mon, Aug 9, 2010 at 11:57 AM, davidtynnhammar <
    [email blocked]<tender%[email blocked]>
    > wrote:

  7. Support Staff 7 Posted by Joel Hooks on 09 Aug, 2010 07:08 PM

    Joel Hooks's Avatar

    There are one or two use cases for PostCostruct, and this likely isn't one of them - it also wouldn't change the unavailability of the property value in the constructor. Using constructor injection is a cleaner solution, so I guess I disagree with Shaun on this one.

  8. 8 Posted by Jason Dias on 09 Aug, 2010 07:15 PM

    Jason Dias's Avatar

    I also agree, in my opinion PostConstruct in a command means the command is doing to much.

    Here is an example of one of my commands,

    package {

    import UserCarRideHeightEvent;
    
    import UserModelManager;
    import UserCar;
    
    import org.robotlegs.mvcs.Command;
    
    public class UpdateUserCarRideHeight extends Command
    {
        [Inject]
        public var event:UserCarRideHeightEvent;
    
        [Inject]
        public var userModelManager:UserModelManager;
    
        override public function execute():void
        {
            var myUserCar:UserCar =  userModelManager.getUserCarByUserCarID(event.userCarID);
            myUserCar.rideHeight = event.rideHeight;
        }
    }

    }

  9. 9 Posted by davidtynnhammar on 10 Aug, 2010 04:56 AM

    davidtynnhammar's Avatar

    ´public class TryMoveCommand extends Command {

    [inject]
    public var m:dModel;
    
    override public function execute():void {
            trace(m);
    }

    This still prints out null's.

    Edit: And I'm not using the constructor anymore.

  10. Support Staff 10 Posted by Ondina D.F. on 10 Aug, 2010 06:12 AM

    Ondina D.F.'s Avatar

    Hi David,

    maybe the [inject] is just a typo in your post, but if you have it like this in your code as well, that could be the cause for the error in your command.
    [Inject] has to be written with a capital I.

    Ondina

  11. 11 Posted by Nikos on 10 Aug, 2010 10:13 AM

    Nikos 's Avatar

    @Jason

    Nice code

  12. 12 Posted by davidtynnhammar on 11 Aug, 2010 06:22 AM

    davidtynnhammar's Avatar

    It was a typo (in my original code that is). How embarrasing.

    Thanks guys!

  13. davidtynnhammar closed this discussion on 11 Aug, 2010 06:22 AM.

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