Multiple event injections in a single command

's Avatar

matan.ube

12 Mar, 2010 02:38 PM

What happens when you map a single command to multiple different events and you want to inject that event into your command?

Can you:

[Inject] public var event1 : EventType1;

[Inject] public var event2 : EventType2;

Lets say EventType1 fired, will event2 be null? If so, one can make an if statement to know what event fired.
OR
Will both have value with the most recent fired event value?

I'll give it a try now.

Thanks,
Matan Uberstein

  1. Support Staff 1 Posted by Till Schneidere... on 12 Mar, 2010 02:48 PM

    Till Schneidereit's Avatar

    Hi Matan,

    that's not possible - at least not exactly the way you're trying to do it.

    Injections are mandatory and an exception will be thrown if one can
    not be satisfied. As events are bound temporarily while they're being
    dispatched, that means that one of your event injections isn't
    satisfied.

    There are two things you can do to still make this work, though:

    - Use an abstract base class and two different concrete command
    classes, each of which injects another event type

    - Use method injection and supply default values for both event classes

    The second option would allow you to use essentially what you have
    now, at the price of some syntactical overhead in the form of methods
    that assign the events to local variables.

    While that would work, I'd heavily recommend using the first option,
    as it makes for a much cleaner design. Alternatively, and perhaps even
    better, you can also use a setup where the two commands bound to
    EventType1 and EventType2, respectively, each perform that part of the
    work that's different and then both dispatch another command, say
    CommonToBothTypesEvent, to which you bind a third command, which then
    performs the work that both commands are supposed to have in common.

    hope that helps,
    till

  2. 2 Posted by Matan on 12 Mar, 2010 03:00 PM

    Matan's Avatar

    Thanks for quick reply and info! :)

    I just tried it, and yes, got the exceptions, as you said. The two command are really simple so I just made to commands. Cleaner and more understandable.

    These two commands just bring up a simple overlay with different copy, but the copy is dependant on the event. Essentially just dispatching a new OverlayEvent, but changes a model before dispatch to update the copy inside the overlay. So not worth making an abstract class for two lines. ;-)

    First time I came across this situation, thought I'll ask someone.

    Thanks!

  3. 3 Posted by jnt on 17 Mar, 2010 08:38 PM

    jnt's Avatar

    Unfortunately, I couldn't get two different events (from different classes) to trigger the same command (and are being injected). Both event classes derive from flash.events.Event, so they share a common abstract base class.

    [Inject] public var injectedEvent:Event;

    does not work. Please help!

  4. Support Staff 4 Posted by Till Schneidere... on 18 Mar, 2010 10:28 AM

    Till Schneidereit's Avatar

    But wasn't your initial question about how to inject into one of two
    *concretely typed* variables based on what event was dispatched?

    Events are always mapped to they concrete class and can only be
    injected into commands with exactly that type, not their super class.
    Unfortunately, there's no way to implement injection into variables
    typed with a super class of your dependency that wouldn't be insanely
    slow.

    Again: Use two different commands that extend a common base class and
    create two different injection points in those. If you use a setter,
    you can assign to a variable defined in the base class that's then
    used in the code paths shared by both commands.

  5. 5 Posted by Matan Uberstein on 18 Mar, 2010 10:33 AM

    Matan Uberstein's Avatar

    Hey tschneidereit,

    That's someone else! Not me! He's got a different problem.

  6. Support Staff 6 Posted by Till Schneidere... on 18 Mar, 2010 10:57 AM

    Till Schneidereit's Avatar

    Ah, sorry for that - I assumed that it's just another email address
    for the same person.

    @jnt: Please open a new discussion for your problem, otherwise it gets
    hard to follow what's going on.

  7. 7 Posted by jnt on 18 Mar, 2010 09:28 PM

    jnt's Avatar

    Thanks for the response. You're right, my problem was a little different. Got it to work with an abstract base class derived from the generic Event class. I had to dispatch the events as follows (maybe a no-brainer, I'm just posting in case somebody else might be looking for this, I think it's not too off-topic):

    dispatch( new AbstractEvent(ConcreteEventClass1.SOME_EVENT, { foo:2 }) );

    dispatch( new AbstractEvent(ConcreteEventClass2.ANOTHER_EVENT, { foo:3}) );

    The command's injection target variable for the event has then to be typed as 'AbstractEvent' - also use this class for the eventClass parameter of the 'commandMap.mapEvent' method.

  8. 8 Posted by Nikos on 06 Oct, 2010 12:57 PM

    Nikos 's Avatar

    Would it be a good idea to use contruction injection in the commands for events rather than metadata based property injection?

  9. Support Staff 9 Posted by Shaun Smith on 06 Oct, 2010 01:07 PM

    Shaun Smith's Avatar
  10. Stray closed this discussion on 18 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