Invalid injections in ItemRenderer

maciekrei's Avatar

maciekrei

28 Jan, 2013 03:20 PM

I'm injecting some service singleton in my custom ItemRenderer. Or I'm trying to do so... My injection object is always null :/

I'm using Robotlegs v1.5.2. Injections in my mediators and commnads work perfectly fine. But when it comes to rendered items in lists, they act like out of context. I checked spelling, I've added commands to my compiler to not strip code out of metadata - everything seems fine, but injection still doesnt work.

This occurs also in other classes. In my model I have some objects with custom structures for storing data. I can inject in model without problem, but I can't inject anything in that object class, although it is a property of model.

Am I missing something or injection should only work in classes that are extending robotlegs framework? If thats the case, then injection for classes extending ItemRenderer seems to be impossible :/

  1. Support Staff 1 Posted by Shaun Smith on 28 Jan, 2013 08:13 PM

    Shaun Smith's Avatar

    Hello,

    Maybe this will help by explaining what is going on under the hood:

    The Injector is simply an object factory. You give it some rules and it can create objects based on those rules and the metadata it comes across whilst creating those objects.

    It can only know about the instances that it creates, or those that you tell it about. If you "new() up" an object yourself the Injector won't have a clue. Likewise, it won't know anything about your ItemRenderers.

    Mediators receive injections because the MediatorMap uses the Injector to injectInto() them after creating them. Likewise with Commands. This has nothing to with extending framework classes, and everything to do with telling the injector about those instances so that it can inject into them.

    For example:

    class Car implements IVehicle {
        [Inject]
        public var engine:IEngine;
    }
    
    class HyperdriveEngine implements IEngine {
    }
    
    injector.map(IEngine).toType(HyperdriveEngine);
    injector.map(IVehicle).toSingleton(Car);
    
    var vehicle:IVehicle = injector.getInstance(IVehicle);
    

    We told the injector how to construct IEngine instances and how to construct IVehicle instances. When we ask it for an IVehicle it reflects over that class, sees the IEngine injection point and knows that it has to create one (luckily we've already told it how to do that).

    We also told it that there should only be one IVehicle, so any future requests for that will get back the same IVehicle instance. For the IEngine we decided that each request gets a new instance of IEngine.

    Sometimes we already have an existing object and all we want to do is inject dependencies into it. For example:

    var car:IVehicle = new Car();
    injector.injectInto(car);
    

    The injector will reflect over that Car instance, and supply any required dependencies. However, as we created it ourselves, that instance will not be available for anyone else who might need an IVehicle from the Injector.

    Essentially you need some way to injectInto() your Item Renderers. We've built utilities that watch the stage, just like the MediatorMap does, and respond in some way to specific view components.

    In Robotlegs 2 you'd use the ViewProcessorMap:

    viewProcessorMap.map(MyItemRenderer).toInjection();
    

    In Robotlegs 1 you'd use the ViewMap:

    viewMap.mapType(MyItemRenderer);
    

    The main point to take away is that the Injector is not magical by any means. It simply uses rules and reflection data to construct and link objects together. You have to tell it what to do, how to do it, and what to do it on.

    Hope that helps!

  2. 2 Posted by maciekrei on 28 Jan, 2013 08:49 PM

    maciekrei's Avatar

    Thanku you very much for your answer! It was plain and comprehensive. Your descrption of mapping process was something I did missing and wasn't fully aware of it. I am greatly satisfied!

    Shaun, you and rest of Robotlegs support team are doing great job with your quick and thoughtful answers. Keep it going! :)

  3. Support Staff 3 Posted by Shaun Smith on 28 Jan, 2013 08:52 PM

    Shaun Smith's Avatar

    It's a pleasure, glad it was helpful! I'm going to resolve/close this issue, but if you have further questions you can re-open it or post a new question.

  4. Ondina D.F. closed this discussion on 30 Jan, 2013 03:02 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