tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/607-dispatching-signals-from-the-model-and-having-mediators-listen-to-themRobotlegs: Discussion 2011-07-21T15:13:12Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/86633502011-07-17T08:32:29Z2011-07-17T08:32:29ZDispatching signals from the model and having mediators listen to them.<div><p>Hi Ryan,</p>
<p>It all depends what you're trying to achieve - but certainly
it's worth knowing how to do it so you can choose the right
approach for your app.</p>
<p>If your signal is a property of the model you'll need to map it
for injection in your context or a command. A command is easier in
some ways as injecting the model into the command will cause it to
be created.</p>
<p>Your signal will need to be strongly typed for injection. And
you mediator will need to extend SignalMediator: <a href=
"https://github.com/Stray/robotlegs-utilities-SignalMediator">https://github.com/Stray/robotlegs-utilities-SignalMediator</a></p>
<p>SignalMediator gives you a SignalMap equivalent to EventMap and
means that your Signal listeners will automatically get cleaned up
when your view leaves the stage - just like with events.</p>
<p>To map the Signal as a singleton (given that it is a property of
your model):</p>
<pre>
<code>injector.mapValue(StrongTypedSignal, someModel.aSignal);</code>
</pre>
<p>Then you just inject this into your mediator and use it as per
the instructions on the SignalMediator page.</p>
<p>If you choose to do this in a Command you can map the command to
the STARTUP_COMPLETE event. If you choose to do it in the context
you'll need to make sure the model is awake, like this:</p>
<pre>
<code>injector.mapSingletonOf(ISomeModel, SomeModel);
var modelInstance:ISomeModel = injector.getInstance(ISomeModel);
injector.mapValue(StrongSignalType, modelInstance.aSignal);</code>
</pre>
<p>Personally I mix Signals and Events in my projects. I think they
each have strengths and I like the flexibility I get from choosing
between them for each use case.</p>
<p>I hope this helps, let me know if you're still fuzzy - I can
expand on any areas that didn't make sense to you.</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/86633502011-07-18T03:59:00Z2011-07-18T03:59:00ZDispatching signals from the model and having mediators listen to them.<div><p>Hi Stray,</p>
<p>This is great, thanks a lot for the explanation! I'll give these
a try and<br>
see how it works.</p>
<p>I wasn't sure if I should be sticking to one method only
(signals or events,<br>
and not mixing), it's more clear now and I'd also prefer to choose
what's<br>
most appropriate for the situation. Knowing how to do both now is
awesome.</p>
<p>I'll let you know if I have any problems.</p>
<p>Thanks,<br>
Ryan</p>
<p>On Sun, Jul 17, 2011 at 1:32 AM, Stray <<br>
<a href=
"mailto:tender+dfcea1d33d4cffa34fcc769f5213e12ec934ba1cc@tenderapp.com">
tender+dfcea1d33d4cffa34fcc769f5213e12ec934ba1cc@tenderapp.com</a>>
wrote:</p></div>Ryan Yacyshyntag:robotlegs.tenderapp.com,2009-10-18:Comment/86633502011-07-18T20:42:43Z2011-07-18T20:42:43ZDispatching signals from the model and having mediators listen to them.<div><p>Hi Stray,</p>
<p>I understand how to map the signal for injection in my context;
however I<br>
just tried to do the same thing in a command but I'm getting an
error<br>
(Injector is missing a rule to handle injection into property
"dataReadySignal" of object "[object FlickrAuthorsMediator]".) I
must be missing something when trying to map the injector by using
a command but<br>
can't seem to figure out where the problem is.</p>
<p>In my MainContext class I'm injecting other classes and mapping
mediators as<br>
well as mapping a command to the STARTUP_COMPLETE event. In my
command<br>
that's mapped to the STARTUP_COMPLETE event I'm injecting my model
and have<br>
one line in my execute function:
injector.mapValue(DataReadySignal,<br>
model.dataReadySignal);.</p>
<p>Could the problem be order of execution? The mediator that uses
the signal<br>
is mapped before the signal is injected? My code is on github if it
helps to<br>
see what I'm trying to achieve: <a href=
"https://github.com/ryac/rl-flickr">https://github.com/ryac/rl-flickr</a></p>
<p>Thanks!<br>
Ryan</p></div>Ryan Yacyshyntag:robotlegs.tenderapp.com,2009-10-18:Comment/86633502011-07-18T20:48:50Z2011-07-18T20:48:50ZDispatching signals from the model and having mediators listen to them.<div><p>Hi Ryan, if the view already exists and the mediator is mapped
before the signal is mapped then yes, that would cause a
problem.</p>
<p>I'll take a peek tomorrow, but in the mean time order of
operations is key.</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/86633502011-07-18T21:14:46Z2011-07-18T21:14:46ZDispatching signals from the model and having mediators listen to them.<div><p>Thanks Stray, I re-arranged the order and now it's all working
as expected.</p>
<p>Really appreciate your help!</p>
<p>Ryan</p>
<p>On Mon, Jul 18, 2011 at 1:48 PM, Stray <<br>
<a href=
"mailto:tender+dfcea1d33d4cffa34fcc769f5213e12ec934ba1cc@tenderapp.com">
tender+dfcea1d33d4cffa34fcc769f5213e12ec934ba1cc@tenderapp.com</a>>
wrote:</p></div>Ryan Yacyshyntag:robotlegs.tenderapp.com,2009-10-18:Comment/86633502011-07-21T15:12:38Z2011-07-21T15:12:38ZDispatching signals from the model and having mediators listen to them.<div><p>Great - I'll close this for now then. Feel free to reopen this
(or a new thread) if you hit any more bumps.</p></div>Stray