Best practice for receiving non Robotlegs events and dispatching them to the Robotlegs event bus ?

andre's Avatar

andre

10 Jan, 2012 05:24 PM

Hi,

I am new to Robotlegs (recent convert from Swiz).

I have events coming from a non Robotlegs flex project actionscript (non visual) class that I need to listen for and re-dispatch on the Robotlegs event bus.

  1. What is the best place to inject/instantiate my non-RL event listener class into the RL context ?

  2. Should I do this as a Relayer/Delegate or directly in a Command class ?

Thanks !

  1. 1 Posted by Jos on 10 Jan, 2012 05:33 PM

    Jos's Avatar

    I'd think a Model or Service, which is setup to receive these events, and then rebroadcast them would be the way to go. You can do any transformations on the incoming events in the Service, to make sure that they are then "appropriate" for the RL context/project. In this way, even if your "old" project changes, you only have to edit the Service class - the rest of your RL app should be ignorant of the non-RL events/bits.

    My 2 cents.

    Good luck!
    Jos

  2. 2 Posted by andre on 10 Jan, 2012 05:42 PM

    andre's Avatar

    Thanks, Jos.

    But if I have a model which extends Actor and is canonically injected into the RL context, would I have to instantiate the non RL class with New Class() or is there a mechanism to inject the non RL class and still have it successfully communicate its events to a listener on a RL class ?

  3. 3 Posted by Jos on 10 Jan, 2012 06:34 PM

    Jos's Avatar

    Hard to say without knowing more about what your "New Class" is doing. If it can be instanced simply inside the RL class, then i'd do that. I don't think it should be injected anywhere, as its not part of the RL app proper - it, and the Service, are the bridge items between the old and new apps. Or, that is how i'm seeing it. Which might be totally non-sensical wrt your applications architecture! :)

  4. 4 Posted by andre on 10 Jan, 2012 07:49 PM

    andre's Avatar

    Thanks, Jos, I'm almost there.

    So I'm injecting my RL class with injector.mapSingleton( MyRLClass), then initializing it so it adds event listeners for the non-RL class. MyRLClass extends Actor.

    MyRLClass now successfully listens for messages from NonRLClass.

    However, when I try to re-dispatch them on the RL message bus, I see that the dispatcher is null. I would have thought the magic of DI would have stopped this happening ?

    Do I need to inject a dispatcher into MyRLClass and set it to this.dispatcher as part of the init for MyRLClass ? If so, from where do I grab/inject the extant RL dispatcher ?

    Thanks again !!!

  5. 5 Posted by Paul Robertson on 10 Jan, 2012 08:04 PM

    Paul Robertson's Avatar

    You can get access to the event bus (dispatcher) using the
    eventDispatcher property of an Actor or other RL member such as Command
    or Mediator.

    If you want to have it injected into your class, create a public var of
    type IEventDispatcher and add the [Inject] metadata to it. Then,
    instead of using new MyClass() to create the instance use
    injector.instantiate() (from any RL object that has access to the
    IInjector, of course).

    Paul

  6. 6 Posted by Jos on 11 Jan, 2012 12:51 AM

    Jos's Avatar

    Andre - the mapSingleton will only create an instance of MyRLClass lazily. If you are not injecting MyRLClass anywhere else, it will never to created.

    If MyRLClass is extending Actor, you should be getting eventDispatcher for free - you can use the dispatch method to send events.

    If you are getting a null for the dispatcher, then i'd guess that you are not Injecting MyRLClass somehow. Can you put up some example code?

    You can also create an instance of MyRLClass upfront, and "force" the injections:

    var rlClass:MyRLClass = new MyRLClass();
    injector.inject(rlClass);
    injector.mapValue(MyRLClass, rlClass);
    

    Try some of that out, and see if it helps.

  7. 7 Posted by andre on 11 Jan, 2012 07:25 PM

    andre's Avatar

    Thanks, Jos.

    Where is the architecturally correct place to put the code to create MyRLClass ?

  8. 8 Posted by Jos on 11 Jan, 2012 08:06 PM

    Jos's Avatar

    Well, that would depend on your architecture, wouldn't it!

    You could do what i outlined above, with the injector.mapValue in a startup command.

    You'd then use something like the following in your view mediator to "listen" for events that MyRLClass puts out.

    override public function onRegister():void
    {
        eventMap.mapListener( eventDispatcher, MyCustomEvent.DO_STUFF, handleDoStuff );
    }
    

    Or map some commands to the events. Or something else entirely!

  9. Ondina D.F. closed this discussion on 23 Feb, 2012 10:33 AM.

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