EventDispatcher not being injected: eventDispatcher = null

hush's Avatar

hush

29 Oct, 2009 11:00 PM

Hi there!
First of all, I think this is a remarkable framework. I've been following it's evolution for some time and - wow - the work you've done on it in such a short time is really amazing. The docs and examples are great and it's easy to grasp the concepts of Robotlegs by reading them. And - Robotlegs!!! How can you resist a framework with such a cool name! 8]

Ok, now the second thing I'd like to talk about is I'm having a little problem with eventDispatcher being null in my service class. I'm using swc v.0.9.5, compiling with flex sdk in FDT3.
The code looks like this, nothing much:

 var swfAddressService:SwfAddressService = new SwfAddressService();
        injector.mapValue(SwfAddressService, swfAddressService);
        swfAddressService.initialize();

Now, after swfAddressService initializes, it receives CHANGE event from SWFAddress and should pass it to the system like so:

 protected function onAddressChanged(e:SWFAddressEvent):void
    {
         eventDispatcher.dispatchEvent(e);
    }

But, the eventDispatcher is null there. Does it look like injection problem or should i be doing some more stuff?
Thanks ;]

  1. Support Staff 1 Posted by Joel Hooks on 29 Oct, 2009 11:25 PM

    Joel Hooks's Avatar

    In your case you need to manually fire that injection:

    injector.injectInto(swfAddressService)
    swfAddressService.initialize();
    
    because you are manually creating the instance, you need to manually inject the dependencies. The eventDispatcher property will be available immediately.
  2. 2 Posted by hush on 29 Oct, 2009 11:48 PM

    hush's Avatar

    Fantastic, thanks for the fast reply ;]

  3. Support Staff 3 Posted by Till Schneidere... on 30 Oct, 2009 10:57 AM

    Till Schneidereit's Avatar

    Or, to be sure that all eventual injection points are dealt with, you could use
    var swfAddressService:SwfAddressService =
    injector.instantiate(SwfAddressService);

    and be done with it.

  4. 4 Posted by hush on 30 Oct, 2009 11:10 AM

    hush's Avatar

    Great, even more elegant for my case. Thanks a lot! ;]

  5. Till Schneidereit closed this discussion on 30 Oct, 2009 11:18 AM.

  6. hush re-opened this discussion on 28 Nov, 2009 11:28 PM

  7. 5 Posted by hush on 28 Nov, 2009 11:28 PM

    hush's Avatar

    Ok, I'm getting back to this again. (Now using v1.0 swc)
    If I write the following code in my StartupCommand:

    injector.mapSingletonOf(Flashvars, Flashvars);
    var flashvars:Flashvars = injector.instantiate(Flashvars);
    flashvars.data = contextView.root.loaderInfo.parameters;
    

    and later inject flashvars in another command

    [Inject]
    public var flashvars:Flashvars;
    

    then Flashvars gets constructed twice. Is that a normal behaviour?

    PS Congratulations with v1.0, yoohoooo, nice work ;]

  8. Support Staff 6 Posted by Shaun Smith on 28 Nov, 2009 11:46 PM

    Shaun Smith's Avatar

    Interesting. No, that doesn't seem right. I suspect it's a bit of an edge-case, as you're mapping to an interface and then depending on a class (roughly speaking). Could you try:

    injector.mapSingleton(Flashvars);
    var flashvars:Flashvars = injector.instantiate(Flashvars);
    flashvars.data = contextView.root.loaderInfo.parameters;
    
  9. 7 Posted by hush on 28 Nov, 2009 11:58 PM

    hush's Avatar

    Same result, gets constructed twice :P

  10. 8 Posted by hush on 29 Nov, 2009 12:05 AM

    hush's Avatar

    I certainly can write like this and it works:

    var flashvars:Flashvars = new Flashvars();
    flashvars.data = contextView.root.loaderInfo.parameters;
    injector.mapValue(Flashvars, flashvars);
    injector.injectInto(flashvars);
    

    But something tells me that injector.instantiate should also have worked.

  11. Support Staff 9 Posted by Till Schneidere... on 30 Nov, 2009 12:59 AM

    Till Schneidereit's Avatar

    Can you check if you get two different instances of the singletons in
    your injectees? I'm guessing that you don't and everything is working
    as it should (or as best as it can). There's a bug in the Flash Player
    that forces SwiftSuspenders (and every other DI solution supporting
    constructor injection) to create one throw-away instance of each class
    it should instantiate. For more information, see this bug in the Flash
    Player issue tracker: http://bugs.adobe.com/jira/browse/FP-183

  12. 10 Posted by Jos Yule on 07 Dec, 2009 02:41 AM

    Jos Yule's Avatar

    If i can also make a suggestion.

    One trick that i have been using (i'm pretty sure via Joel H), is to create a Command to setup your Model/Service. So, in your context do something like:

    injector.mapSingleton(Flashvars)
    commandMap.mapEvent(ContextEvent.STARTUP_COMPLETE, SetupFlashvarsCommand);
    

    Then, in your SetupFlashvarsCommand:

    [Inject] public var flashvars:Flashvars
    [Inject] public var contextView:DisplayObjectContainer;
    
    public function exectue():void
    {
        flashvars.data = contextView.root.loaderInfo.parameters;
    }
    

    That makes sure that all your dependiancies are taken care of (in your flashvars class, assuming you've used the [Inject] tag around them.

    Help? Hinder?

  13. Support Staff 11 Posted by Shaun Smith on 07 Dec, 2009 03:37 PM

    Shaun Smith's Avatar

    That's a good approach. One small thing: the mvcs abstract command already has a reference to the contextView, so no need to declare it again (I think it would throw an error anyway).

  14. 12 Posted by xisari on 15 Dec, 2009 12:13 PM

    xisari's Avatar

    Maybe I'm a few days too late but here is my suggestion:

    The mapSingleton() command creates an instance of your class and you create an additional one with injector.instantiate(Flashvars)

    You should either create the instance via mapSingleton() and use a command to push the data into it (like Jos suggested) or you create the instance in the context und push the data there.

  15. Shaun Smith closed this discussion on 06 Jan, 2010 11:54 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