tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/5121-why-doesnt-my-signal-trigger-the-command-it-is-mapped-toRobotlegs: Discussion 2018-10-18T16:35:53Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/308263982013-12-31T11:14:09Z2013-12-31T11:14:09ZWhy doesn't my signal trigger the command it is mapped to?<div><blockquote>
<p>Why doesn't my signal trigger the command it is mapped to?</p>
</blockquote>
<p>Because the signal you've created in your view like this:</p>
<pre>
<code>var signal:HelloWorldSignal = new HelloWorldSignal();</code>
</pre>
<p>is a <strong>new</strong> instance of HelloWorldSignal,
different from the one known by the Injector through the
mapping:</p>
<pre>
<code>signalCommandMap
.map(HelloWorldSignal)
.toCommand(HelloWorldCommand)</code>
</pre>
<p>Let the Injector create or get an instance of HelloWorldSignal,
either by injecting the Signal into the class, that needs it:</p>
<pre>
<code>[Inject]
public var helloWorldSignal: HelloWorldSignal;</code>
</pre>
<p>or like this:</p>
<pre>
<code>var helloWorldSignal: HelloWorldSignal = injector.getInstance(HelloWorldSignal);</code>
</pre>
<p>It is possible to inject a signal into a view. You need a
mapping for your signal and the viewProcessorMap to let the
injector know which view needs injection ( see the link below for
details about viewProcessorMap):</p>
<pre>
<code>//injector.map(HelloWorldSignal).asSingleton();
signalCommandMap
.map(HelloWorldSignal)
.toCommand(HelloWorldCommand)
viewProcessorMap.map(HelloWorldView).toInjection();</code>
</pre>
<p>Even though it is possible to trigger a command by dispatching a
signal from a view, I wouldn't recommend this approach. It is
better to let the mediator of a view communicate with other parts
of your application on behalf of its view.</p>
<p>Similar discussions:<br>
<a href=
"http://knowledge.robotlegs.org/discussions/robotlegs-2/8018-why-i-can-inject-a-signal-in-mediator-and-dispantch-but-not-in-view#comment_29936130">
http://knowledge.robotlegs.org/discussions/robotlegs-2/8018-why-i-c...</a><br>
<a href=
"http://knowledge.robotlegs.org/discussions/robotlegs-2/5766-automagic-view-injection#comment_28759520">
http://knowledge.robotlegs.org/discussions/robotlegs-2/5766-automag...</a></p>
<p>Robotlegs-Internals:<br>
<a href=
"https://github.com/robotlegs/robotlegs-framework/wiki/Robotlegs-Internals">
https://github.com/robotlegs/robotlegs-framework/wiki/Robotlegs-Int...</a></p>
<p>ViewProcessorMap:<br>
<a href=
"https://github.com/robotlegs/robotlegs-framework/tree/master/src/robotlegs/bender/extensions/viewProcessorMap">
https://github.com/robotlegs/robotlegs-framework/tree/master/src/ro...</a></p>
<p>Hope that helps<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/308263982013-12-31T12:47:42Z2013-12-31T12:47:42ZWhy doesn't my signal trigger the command it is mapped to?<div><p>Thank you very much, it works. As simple as it is, it slipped my
mind.</p>
<p>I have a follow up question on best practice on using Signals.
In the following mediator I have mixed events and signals. I added
an event listener in the mediator to keep it out of the view and I
didn't use a signal in the view as this also clutters the view in
my mind.</p>
<p>What is considered best practice?</p>
<pre>
<code>package
{
import flash.events.MouseEvent;
import robotlegs.bender.bundles.mvcs.Mediator;
public class HelloWorldMediator extends Mediator
{
[Inject]
public var view:HelloWorld;
[Inject]
public var helloWorldSignal:HelloWorldSignal;
override public function initialize():void
{
view.button.addEventListener(MouseEvent.CLICK,handle_Click);
}
private function handle_Click(e:MouseEvent):void
{
helloWorldSignal.dispatch();
}
}
}</code>
</pre></div>rmhtag:robotlegs.tenderapp.com,2009-10-18:Comment/308263982013-12-31T14:11:07Z2013-12-31T14:11:07ZWhy doesn't my signal trigger the command it is mapped to?<div><p>Glad to hear that it worked!</p>
<p>A good practice would be to dispatch a custom event from the
view, in the handler of MouseEvent.CLICK when the button is
clicked:</p>
<p>dispatchEvent(new SomeEvent (SomeEvent.SOME_TYPE));</p>
<p>The mediator adds a listener like so:</p>
<p>addViewListener(SomeEvent.SOME_TYPE, onSomethingHappened,
SomeEvent);</p>
<p>The custom event should override clone() if you want it to be
re-dispatched by the mediator:</p>
<p>addViewListener(SomeEvent.SOME_TYPE, dispatch, SomeEvent);</p>
<p>The advantage of using addViewListener is that when the mediator
gets destroyed(when the view is removed from stage) the event
listeners will be removed automatically. That is important in case
you want your views/mediators to get garbage collected.</p>
<p>In your example:</p>
<p>view.button.addEventListener(MouseEvent.CLICK,handle_Click);</p>
<p>if you don't remove the listener manually in the overridden
destroy() method of the mediator, the view won't be eligible for
gc, because there will be still a reference to it, keeping it
alive. Same with signals, you'd have to remove the signals manually
inside destroy().</p>
<p>Another advantage of using custom events dispatched from views
is that the mediator doesn't have to care whether the button
dispatches a Mouse or a Touch event. This way your mediators become
portable, you can use the same mediator for a browser, desktop or
mobile app. The views are also encapsulated, portable and don't
depend on any framework or library to be functional.</p>
<p>If you want to use signals, you can take a look at the demo
linked below to see how you can dispatch signals from views, how
the mediator listens to them and so on:</p>
<p><a href=
"https://github.com/robotlegs/robotlegs-demo-TodoList">https://github.com/robotlegs/robotlegs-demo-TodoList</a></p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/308263982013-12-31T22:23:58Z2013-12-31T22:23:58ZWhy doesn't my signal trigger the command it is mapped to?<div><p>Thank you very much for a swift and comprehensive answer.</p></div>rmh