tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/9680-inject-ieventdispatcher-with-namedoesnt-work-for-commandRobotlegs: Discussion 2014-02-10T11:37:38Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/315174162014-02-08T13:19:57Z2014-02-08T13:19:57ZInject IEventDispatcher with name,doesn't work for Command<div><p>Hello,</p>
<p>Your question isn't very clear.</p>
<p><strong>[1]</strong> In the title of your post you said:</p>
<blockquote>
<p>Inject IEventDispatcher with name,doesn't work for Command</p>
</blockquote>
<p><strong>[2]</strong> but then you said:</p>
<blockquote>
<p>thought OpenDocumentCommand won't be triggered, but i was wrong.
my problem is how can i dispatch DocumentEvent.OPEN_DOCUMENT event
and the command doesn't execute.</p>
</blockquote>
<p><strong>[1]</strong> That doesn't work because the
EventCommandMapExtension is using the context's default
IEventDispatcher, so only events that were dispatched on that
shared dispatcher will trigger a command.</p>
<p>Currently, there is no way, I know of, of telling the
EventCommandMap which dispatcher to use when mapping an event to a
command.<br>
You could create an additional EventCommandMap, to which you pass
your named dispatcher, and map IEventCommandMap with a name, so it
doesn't override the mapping for the default one, to the value of
your new EventCommandMap.</p>
<p>Something like this:</p>
<pre>
<code>var someDispatcher:EventDispatcher = new EventDispatcher();
injector.map(IEventDispatcher, "someDispatcher").toValue(someDispatcher);
var specialCommandMap:EventCommandMap = new EventCommandMap(context, someDispatcher);
injector.map(IEventCommandMap, "specialCommandMap").toValue(specialCommandMap);
specialCommandMap.map(SomeEvent.TRIGGER_SOMECOMMAND, SomeEvent).toCommand(SomeCommand);</code>
</pre>
<p>But, I think, this can become messy in case you need several
named event dispatchers.</p>
<p><strong>[2]</strong> I don't understand this question. Are you
asking why the command is triggered or why it isn't triggered?</p>
<p>If you're asking why it is triggered despite the fact the event
dispatcher is not the context's default event dispatcher, that can
happen if you're dispatching the event from within a Mediator using
dispatch()</p>
<p>dispatch( new
DocumentEvent(DocumentEvent.OPEN_DOCUMENT,document));</p>
<p>dispatch () will trigger the command because a Mediator
(<a href="https://github.com/robotlegs/robotlegs-framework/blob/master/src/robotlegs/bender/bundles/mvcs/Mediator.as#L31">https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...</a>]
is injected with the shared event dispatcher and when you call
dispatch() it dispatches the event on that share event
dispatcher:</p>
<pre>
<code>protected function dispatch(event:Event):void
{
if (eventDispatcher.hasEventListener(event.type))
eventDispatcher.dispatchEvent(event);
}</code>
</pre>
<p>If you're asking how to prevent the execution of the command
when you dispatch an event to which the command has been mapped,
you either:</p>
<ul>
<li>unmap the command before dispatching the event</li>
</ul>
<p>commandMap.unmap(DocumentEvent.OPEN_DOCUMENT,
DocumentEvent).fromCommand(OpenDocumentCommand)</p>
<ul>
<li><strong>set once to true</strong> = the command runs only
once</li>
</ul>
<p>eventCommandMap.map(
DocumentEvent.OPEN_DOCUMENT,DocumentEvent).toCommand(
OpenDocumentCommand ).once(true);</p>
<ul>
<li>or you simply use another event type, which is preferable, in
case you need to trigger that command again</li>
</ul>
<p>If I didn't answer your question, please be more clear about
what it is you want to achieve. :)</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/315174162014-02-08T13:50:20Z2014-02-08T13:50:20ZInject IEventDispatcher with name,doesn't work for Command<div><p>sorry , my title made you confused.</p>
<p>hmm, I want to create another shared EventDispatcher which will
not dispatch event to command despite the event is mapping for
command. so I Inject a EventDispatcher with name . when i use my
custom EventDispatcher dispatch a mapping event, the command still
execute, i don't understand that. i didn't dispatch event from
mediator by dispatch function.</p>
<p>in fact, i had considered use another event type, but not
sure if it is a good way.<br>
really appreciate for your detailed answer, i got a lot of
help.</p></div>foodyitag:robotlegs.tenderapp.com,2009-10-18:Comment/315174162014-02-08T14:34:49Z2014-02-08T14:34:49ZInject IEventDispatcher with name,doesn't work for Command<div><blockquote>
<p>when i use my custom EventDispatcher dispatch a mapping event,
the command still execute, i don't understand that.</p>
</blockquote>
<p>Well, that's impossible because of what I've explained in the
first part [1]</p>
<p>There must be something else going on. Can you paste the code
for the method where you dispatch the event to trigger the command
on the default event dispatcher? Maybe that function/method is
called from somewhere else in your app for some reasons?</p>
<blockquote>
<p>in fact, i had considered use another event type, but not
sure if it is a good way.</p>
</blockquote>
<p>Why is it not a good way? It seems you want to use your
OPEN_DOCUMENT event for 2 different actions: one is for triggering
the command, and the other for something else. Using another event
type for the second action makes your intentions clear. What should
happen when you dispatch OPEN_DOCUMENT on the custom event
dispatcher? Who is listening for that event and what should that
class do? I assume "model_to_view" dispatcher is injected into a
Model, which dispatches the event on that custom dispatcher when
its data changes, so it would be good to use an event type that is
describing what just happened.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/315174162014-02-08T15:01:55Z2014-02-08T15:01:55ZInject IEventDispatcher with name,doesn't work for Command<div><blockquote>
<p>There must be something else going on. Can you paste the code
for the method ?>where you dispatch the event to trigger the
command on the default event dispatcher? >Maybe that
function/method is called from somewhere else in your app for some
>reasons?</p>
</blockquote>
<p>you are right, I checked my code again , I had mapped a wrong
event type for the command. Changing event type is better than
create another shared eventDispatcher ,right ?</p></div>foodyitag:robotlegs.tenderapp.com,2009-10-18:Comment/315174162014-02-08T16:18:31Z2014-02-08T16:18:31ZInject IEventDispatcher with name,doesn't work for Command<div><blockquote>
<p>Changing event type is better than create another shared
eventDispatcher ,right ?</p>
</blockquote>
<p>Yes, I think it's better and also easier to implement.<br>
It is more efficient and more clear to have 2 different event types
detonating 2 different actions than having 2 event dispatchers
dispatching the same event type.</p>
<p>In your scenario, if a Mediator is listening for OPEN_DOCUMENT
event that is also mapped to a Command, you'd have to inject the
named dispatcher and add an explicit listener to it.<br>
But, if you'd dispatch the event with another event type on the
default dispatcher, the only thing you'd need to do would be :<br>
addContextListener(DocumentEvent.SOME_DATA_CHANGED,
onSomeDataChanged, DocumentEvent);<br>
No need for named injections in the Model (or other class that is
dispatching on the custom dispatcher) and Mediator anymore.</p>
<p>So, your custom DocumentEvent should contain different constants
for different actions..</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/315174162014-02-09T00:51:20Z2014-02-09T00:51:20ZInject IEventDispatcher with name,doesn't work for Command<div><p>thank you so much, now i know how to deal this situation very
well. i 'll I will continue to enjoy rl2. ^ ^</p></div>foodyitag:robotlegs.tenderapp.com,2009-10-18:Comment/315174162014-02-10T11:36:58Z2014-02-10T11:36:58ZInject IEventDispatcher with name,doesn't work for Command<div><p>You're welcome :)</p></div>Ondina D.F.