Getting The Global Event Dispatcher

dehru.cromer's Avatar


01 Mar, 2011 10:47 PM


I am working with graniteDS. Many of my model objects have their lifecycles managed by granite. They are not constructed by robotlegs so they don't get the eventDispatcher injected...even if they extend Actor. What's the best way to get the eventDispatcher from a non-robotlegs managed class?


  1. 1 Posted by Michal Wroblews... on 02 Mar, 2011 08:22 AM

    Michal Wroblewski's Avatar

    You can pass any Injects to your non-robotlegs classes with [Inject] metatag. Then you need to somehow pass those instances to a command or something else with injected IInjector. You can satisfy all inject dependencies with injector.injectInto(yourNonRobotlegsInstance).

    Anyone got better ideas?

  2. Support Staff 2 Posted by Stray on 02 Mar, 2011 11:09 AM

    Stray's Avatar

    The other option is to do conventional injection.

    Have a factory which does get injected, and then just set the eventDispatcher property on your actors manually:

    thing.eventDispatcher = eventDispatcher;

    This is possibly better from a performance point of view, because if you know this is the only injection that needs to be fulfilled you don't have the expense of the describeType operation on the class being injected.

  3. 3 Posted by dehru.cromer on 02 Mar, 2011 05:10 PM

    dehru.cromer's Avatar

    Thanks for the suggestions. I was using the FlexGlobals to get the instance of the application context object's event dispatcher. It seemed like there should be a better way. I'll try these suggestions.


  4. 4 Posted by dehru.cromer on 02 Mar, 2011 07:29 PM

    dehru.cromer's Avatar

    from a non-robotlegs class, what's the best way to get the app contexts injector...that's the one I need right? It looks like it's protected.

    Also, when these non-robotlegs model classes get instantiated, when should I do the injection. Constructor?

    I don't think I can use a factory to instantiate these classes since they are managed by granite ds. Or are you suggesting pass them through a method that puts the eventDispatcher on them?


  5. Support Staff 5 Posted by Stray on 02 Mar, 2011 07:38 PM

    Stray's Avatar

    Yup - that's what I meant. The factory would accept the objects as input, and give them back to you with the eventDispatcher populated.

    Provided the factor extends Actor and is created using the injector, you'll have access.

    The eventDispatcher shouldn't be protected generally - it's available in any Command, Actor etc. So you could also potentially do this in a command, if you passed a Vector of the objects as payload.

     public function wiringEvent:ApplicationWiringEvent;
     public override function execute():
         var objectsToWire:Vector.<Actor> = wiringEvent.objectsToWire;
         var iLength:uint = objectsToWire.length;
         for (var i:int = 0; i < iLength; i++)
             objectsToWire[i].eventDispatcher = eventDispatcher;

    That would do it.

  6. 6 Posted by Michal Wroblews... on 02 Mar, 2011 07:40 PM

    Michal Wroblewski's Avatar

    The most dirty way of doing it (do it only when you won't succeed with other methods) is to place a static signleton method providing you injector out from the application context class. But please don't tell anything @coding_unicorn about it ;)

  7. Support Staff 7 Posted by Stray on 02 Mar, 2011 07:41 PM

    Stray's Avatar

    Michal - go sit on the naughty step!

    That is really, REALLY dirty. ;)

  8. 8 Posted by dehru.cromer on 02 Mar, 2011 08:38 PM

    dehru.cromer's Avatar

    Hahaha. Michael. I just coded that up while awaiting your response...I'll meet you on the naughty step.

    So if I create a utility factory object to do this...let's say I inject one on my main application or something which then gives it the correct eventDispatcher from RL. What's the best way to get the instance of this factory object in non-robotlegs instantiated classes?

    I can't figure out how to avoid: FlexGlobals.topLevelApplication.factoryObject.injectDispatcher( objectThatNeedsDispatcher );

    I like the event based mechanism you suggest, but the problem I have is I don't have an event dispatcher in my model objects to dispatch the event without going to the naughty step. :-(

    Sits down on step...

  9. 9 Posted by Stray on 02 Mar, 2011 09:08 PM

    Stray's Avatar


    So... what access points do you have to your objects that come out of the creator?

    There must be a place where you initiate something?

  10. 10 Posted by dehru.cromer on 02 Mar, 2011 09:23 PM

    dehru.cromer's Avatar

    Yes. I have the constructor available to do any initialization in. As long as I call super() then graniteds is happy.

  11. 11 Posted by Stray on 02 Mar, 2011 10:01 PM

    Stray's Avatar

    Sorry - I meant the place where you say "new ThingThatExtendsActor()"

    ideally you'd pass that eventDispatcher down to there - and not just bang a six inch nail (static) in. But it's hard to know what your options are for passing it along gracefully.

    What I would say is that if you're going to take the naughty route, at least double-decouple it.

    Meaning that you should chain your code to something like:


    And then also give yourself a static method for setting the eventDispatcher. That way you can still provide an alternative eventDispatcher easily enough.

    Gross, but less rigid than accessing the property directly.

  12. 12 Posted by dehru.cromer on 02 Mar, 2011 10:15 PM

    dehru.cromer's Avatar

    Michal and Stray. Thanks for all the options. I'll probably use the decoupled naughty chair so I can sit on it at my desk and get my work done.

  13. Stray closed this discussion on 10 Mar, 2011 03:23 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? 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