Some questions

creynders's Avatar

creynders

26 Jan, 2010 10:09 AM

There is one thing which is not clear to me:

I want to use an injected property value in my initializing code for a certain class, but the value seems to be null. However 'later' on it does have a value, so the injection does seem to work, but not immediately. Is this how it is supposed to be? And if so, how do I solve the problem that I need the value in the init code block?

Hope you guys can help!!

  1. Support Staff 1 Posted by Till Schneidere... on 26 Jan, 2010 10:35 AM

    Till Schneidereit's Avatar

    Hi Camille,

    I suppose by "init block" you mean the constructor, right? If so,
    you've got three options:

    1: Use constructor injection. If your class is created by the injector
    - either because it is itself injected into some other class, or
    because you're using injector.instantiate(MyClass) to instantiate it -
    you can just add constructor arguments that then get supplied by the
    injector.

    2: Use the [PostConstruct] metadata annotation to let the injector
    invoke a method of your class after all injections have been applied.

    3: Inject into a setter that then initializes you class.

    You can find more documentation about all of these approaches in the
    SwiftSuspenders README at
    http://github.com/tschneidereit/SwiftSuspenders

    cheers,
    till

  2. 2 Posted by creynders on 26 Jan, 2010 12:06 PM

    creynders's Avatar

    Thanks a lot, I used the setter injection to initialize the class!

    Another question: I mapped a Command to be executed on an event of class AEvent, but the same Command should be mapped to events of class BEvent and CEvent, all subclassing flash.events.Event, yet if I have an injectable property 'event' of type 'Event' I get following error:

    Error: Injector is missing a rule to handle injection into target [object ErrorOccurred]. Target dependency: flash.events::Event

    Isn't it possible to let an injectable property accept instances of subclasses?

  3. Support Staff 3 Posted by Till Schneidere... on 26 Jan, 2010 12:15 PM

    Till Schneidereit's Avatar

    Glad that worked out for you!

    As for your events problem:
    While it is possible to map from one class in the injector to another
    (i.e., the injector could be instructed to return an instance of
    AEvent when asked for Event), it's unfortunately not possible for
    Robotlegs to automatically set up these mappings.

    The way the event injection works is by temporarily creating an
    injector mapping for the event's class before instantiating the
    command and then removing that mapping afterwards.
    For you usecase to be supported, Robotlegs would have to create
    temporary mappings not only for the event's actual class, but also for
    all its inherited classes - right up to Object.

  4. 4 Posted by creynders on 26 Jan, 2010 03:24 PM

    creynders's Avatar

    Ok, so that's just not possible. Guess I'll have to find a (clean) work-around then.
    Just to make sure I understand correctly:
    for the injector there's no connection whatsoever between a class and it's subclasses, right?

    Coming back to the original problem, while it's easy enough to solve, wouldn't it be better to have an automatically called 'onRegister' method for all Actor instances ? Just like in mediators? Or is there a good reason not to do this?

  5. 5 Posted by creynders on 26 Jan, 2010 03:26 PM

    creynders's Avatar

    And thanks for all the help BTW.
    RL really rocks!! It's my favorite AS framework by far and I've been advertising it a lot!

  6. Support Staff 6 Posted by Till Schneidere... on 26 Jan, 2010 03:42 PM

    Till Schneidereit's Avatar

    > for the injector there's no connection whatsoever between a class and it's subclasses, right?

    Yes, that's correct. Everything else would have undesirable
    side-effects and be to heavy on performance.

    > Coming back to the original problem, while it's easy enough to solve, wouldn't it be better to have an automatically called 'onRegister' method for all Actor instances ? Just like in mediators? Or is there a good reason not to do this?

    Basically, it's just not needed. You can achieve the exact effect of
    Mediator's onRegister by using [PostConstruct]. The mediator has
    onRegister to work around Flex's UIComponent taking some time to be
    fully initialized.

    > And thanks for all the help BTW.
    > RL really rocks!! It's my favorite AS framework by far and I've been advertising it a lot!

    Yeah, Shaun has built something really great with Robotlegs. It's
    awesome to see it taking off!

  7. Stray closed this discussion on 13 Feb, 2011 07:06 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