tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/775-best-practice-for-receiving-non-robotlegs-events-and-dispatching-them-to-the-robotlegs-event-busRobotlegs: Discussion 2018-10-18T16:35:35Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/128033802012-01-10T17:33:31Z2012-01-10T17:33:32ZBest practice for receiving non Robotlegs events and dispatching them to the Robotlegs event bus ?<div><p>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.</p>
<p>My 2 cents.</p>
<p>Good luck!<br>
Jos</p></div>Jostag:robotlegs.tenderapp.com,2009-10-18:Comment/128033802012-01-10T17:42:40Z2012-01-10T17:42:40ZBest practice for receiving non Robotlegs events and dispatching them to the Robotlegs event bus ?<div><p>Thanks, Jos.</p>
<p>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 ?</p></div>andretag:robotlegs.tenderapp.com,2009-10-18:Comment/128033802012-01-10T18:34:13Z2012-01-10T18:34:14ZBest practice for receiving non Robotlegs events and dispatching them to the Robotlegs event bus ?<div><p>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! :)</p></div>Jostag:robotlegs.tenderapp.com,2009-10-18:Comment/128033802012-01-10T19:49:25Z2012-01-10T19:49:25ZBest practice for receiving non Robotlegs events and dispatching them to the Robotlegs event bus ?<div><p>Thanks, Jos, I'm almost there.</p>
<p>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.</p>
<p>MyRLClass now successfully listens for messages from
NonRLClass.</p>
<p>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 ?</p>
<p>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 ?</p>
<p>Thanks again !!!</p></div>andretag:robotlegs.tenderapp.com,2009-10-18:Comment/128033802012-01-10T20:04:48Z2012-01-10T20:04:48ZBest practice for receiving non Robotlegs events and dispatching them to the Robotlegs event bus ?<div><p>You can get access to the event bus (dispatcher) using the<br>
eventDispatcher property of an Actor or other RL member such as
Command<br>
or Mediator.</p>
<p>If you want to have it injected into your class, create a public
var of<br>
type IEventDispatcher and add the [Inject] metadata to it.
Then,<br>
instead of using new MyClass() to create the instance use<br>
injector.instantiate() (from any RL object that has access to
the<br>
IInjector, of course).</p>
<p>Paul</p></div>Paul Robertsontag:robotlegs.tenderapp.com,2009-10-18:Comment/128033802012-01-11T00:51:39Z2012-01-11T00:51:39ZBest practice for receiving non Robotlegs events and dispatching them to the Robotlegs event bus ?<div><p>Andre - the mapSingleton will only create an instance of
MyRLClass lazily. If you are not injecting MyRLClass anywhere else,
it will never to created.</p>
<p>If MyRLClass is extending Actor, you should be getting
eventDispatcher for free - you can use the dispatch method to send
events.</p>
<p>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?</p>
<p>You can also create an instance of MyRLClass upfront, and
"force" the injections:</p>
<pre>
<code>var rlClass:MyRLClass = new MyRLClass();
injector.inject(rlClass);
injector.mapValue(MyRLClass, rlClass);</code>
</pre>
<p>Try some of that out, and see if it helps.</p></div>Jostag:robotlegs.tenderapp.com,2009-10-18:Comment/128033802012-01-11T19:25:30Z2012-01-11T19:25:30ZBest practice for receiving non Robotlegs events and dispatching them to the Robotlegs event bus ?<div><p>Thanks, Jos.</p>
<p>Where is the architecturally correct place to put the code to
create MyRLClass ?</p></div>andretag:robotlegs.tenderapp.com,2009-10-18:Comment/128033802012-01-11T20:06:57Z2012-01-11T20:06:58ZBest practice for receiving non Robotlegs events and dispatching them to the Robotlegs event bus ?<div><p>Well, that would depend on your architecture, wouldn't it!</p>
<p>You could do what i outlined above, with the injector.mapValue
in a startup command.</p>
<p>You'd then use something like the following in your view
mediator to "listen" for events that MyRLClass puts out.</p>
<pre>
<code>override public function onRegister():void
{
eventMap.mapListener( eventDispatcher, MyCustomEvent.DO_STUFF, handleDoStuff );
}</code>
</pre>
<p>Or map some commands to the events. Or something else
entirely!</p></div>Jos