commandMap.execute() and injection of a property of type String

tom's Avatar

tom

08 Dec, 2010 01:54 PM

there is a command like

public final class JsAlertCommand extends ModuleCommand
{
    [Inject]
    public var text : String;

    [Inject]
    public var eiService : IExternalInterfaceService;

    override public function execute() : void {
        eiService.alert(text);
    }
}

when the command is executed

commandMap.execute(JsAlertCommand, 'foobaz!');

the injector gets angry

Error: Injector is missing a rule to handle injection into target [object JsAlertCommand]. Target dependency: String
at org.swiftsuspenders.injectionpoints::PropertyInjectionPoint/applyInjection( [/Users/tom/Repositories/SwiftSuspenders/src/org/swiftsuspenders/injectionpoints/PropertyInjectionPoint.as:42]

what is wrong?

i've compiled SwiftSuspenders-v1.5.1.swc and robotlegs-framework-v1.4.0.swc using flex 4.5.

thanks!

  1. 1 Posted by Stray on 08 Dec, 2010 02:24 PM

    Stray's Avatar

    Your text / string injection isn't being fulfilled.

    I'm guessing that you've got yourself confused about what gets injected in the normal command map.

    If this is the signal command map then the signal dispatch parameters do get injected directly.

    But for the normal command map, the event that triggered the command, and not the properties of the event, is injected.

    so

        [Inject]
        public var alertEvent:AlertEvent;

    or whatever your event is.

    and then...

        eiService.alert(alertEvent.text);

    Hope that fixes it for you!

    Stray

  2. 2 Posted by tom on 08 Dec, 2010 02:32 PM

    tom's Avatar

    you know what, this command is not mapped at all, but getting called though. strange!
    in addition if i add this mapping in the context

    injector.mapSingleton(String);
    

    the dependency is fulfilled.

    but now even stranger, the command is constructed in a endless loop every few hundred miliseconds...

  3. 3 Posted by Stray on 08 Dec, 2010 02:45 PM

    Stray's Avatar

    Hi Tom - mapping a Singleton for a base class is bound to make things go a bit crazy :)

    You could equally do

        injector.mapValue(String, "Hello world")

    and the injection would be fulfilled with "Hello world" but my assumption is that your alert message is somehow related to the event that triggers the alert?

    The command cannot run unless either it is being mapped or you are calling it manually.

    If you have more than one event with the same string type, and you haven't mapped your command against the event class too, then you could see false firings.

    If you want to paste your actual mappings in I'll be happy to take a closer look.

  4. 4 Posted by tom on 08 Dec, 2010 02:52 PM

    tom's Avatar

    yes - i need to display a javascript alert popup (using ExternalInterface). i do have a service that wraps around ExternalInterface so my modules can utilize it by passing events on the module bus.
    i guess i should use an event and simply map the command accordingly.
    but i dislike the fact that i should have to write more code, so i tried to use commandMap.execute().

  5. 5 Posted by Stray on 08 Dec, 2010 02:59 PM

    Stray's Avatar

    More/less code should not really be the basis of your design decisions - there are many, many things to consider that come further up the list than "typing".

    I imagine chasing down this bug has been a lot more time and energy consuming than doing it the "more code" way? I think it takes me about 3 minutes to create a custom Event... ;)

  6. 6 Posted by tom on 08 Dec, 2010 03:00 PM

    tom's Avatar

    ok, lesson learnt. thanks a lot for your support!

  7. Support Staff 7 Posted by Shaun Smith on 08 Dec, 2010 09:46 PM

    Shaun Smith's Avatar

    Hi Tom,

    You should be able to do this:

    public final class JsAlertCommand extends ModuleCommand
    {
        [Inject(name="MyText")]
        public var text : String;
    
        [Inject]
        public var eiService : IExternalInterfaceService;
    
        override public function execute() : void {
            eiService.alert(text);
        }
    }
    

    And then:

    commandMap.execute(JsAlertCommand, 'foobaz!', String, 'MyText');
    

    The command does not need to be mapped for this to work. When injecting primitives (String, Number, int etc) it's best to use named injection. Hope that helps!

  8. 8 Posted by tom on 17 Dec, 2010 05:31 PM

    tom's Avatar

    this is working like a charm. thanks a lot!

  9. Stray closed this discussion on 11 Feb, 2011 10:47 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