tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/1248-no-subjectRobotlegs: Discussion 2018-10-18T16:35:44Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/207176462012-11-11T19:45:26Z2012-11-11T19:45:26ZNo subject<div><p>IMO your best bet is to make a new command. Commands are meant
to be stateless singular actions. As soon as they start waiting for
a response, they are doing too much work.</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/207176462012-11-12T06:55:38Z2012-11-12T06:55:38ZNo subject<div><p>You mean, catching the ServiceEvent dispatches in "context" and
then mapping a new command there ?</p></div>vishwas.gagranitag:robotlegs.tenderapp.com,2009-10-18:Comment/207176462012-11-12T13:48:16Z2012-11-12T13:48:16ZNo subject<div><p>That approach would be difficult. As services like XMLLoader are
commonly used in so many views. Then how come i can bind an event
like Service.LoadComplete to some command in the context.</p></div>vishwas.gagranitag:robotlegs.tenderapp.com,2009-10-18:Comment/207176462012-11-12T16:15:51Z2012-11-12T16:15:51ZNo subject<div><p>Listen for the native event within your service class, then
dispatch a<br>
custom event for the success or failure of that particular service.
If<br>
you need to do something to your model and/or service layer when
the<br>
service returns, map commands in the context for the custom event.
If<br>
you want to do something in the view when the service returns, map
a<br>
listener in a mediator for the custom event.</p>
<p>There are also alternatives, although what Joel is recommending
is<br>
closest to what I would call "traditional" Robotlegs MVCS.</p>
<p>Other people have come up with mechanisms for listening to an
event in<br>
a command -- in this thread, item <a title="Discussion #8" href=
"/discussions/questions/8">#8</a> I talk about some of the<br>
approaches people use:<br>
<a href=
"http://knowledge.robotlegs.org/discussions/questions/255-lifecycle-of-a-command">
http://knowledge.robotlegs.org/discussions/questions/255-lifecycle-...</a></p>
<p>One key bit is to register a listener in the command's
execute()<br>
method, then call the commandMap's detain() method to keep the
command<br>
from getting garbage collected:</p>
<p>public function execute():void<br>
{</p>
<pre>
<code>myService.start();
myService.addEventListener(MyServiceEvent.COMPLETE,</code>
</pre>
<p>completeHandler);</p>
<pre>
<code>commandMap.detain(this);</code>
</pre>
<p>}</p>
<p>In your listener, once you've gotten a response, call the
commandMap's<br>
release() method to free the command for garbage collection:</p>
<p>private function completeHandler(event:MyServiceEvent):void<br>
{</p>
<pre>
<code>myService.removeEventListener(MyServiceEvent.COMPLETE,</code>
</pre>
<p>completeHandler);</p>
<pre>
<code>commandMap.release(this);</code>
</pre>
<p>}</p>
<p>Note that I would still recommend using a custom event
dispatched from<br>
your service, and I would still recommend <em>against</em> doing
additional<br>
work on models etc. in the event handler -- since then your
command<br>
does two things, not just one.</p>
<p>Paul</p></div>Paul Robertsontag:robotlegs.tenderapp.com,2009-10-18:Comment/207176462012-11-12T17:20:28Z2012-11-12T17:28:20ZNo subject<div><p>"Listen for the native event within your service class, then
dispatch a custom event for the success or failure of that
particular service. If<br>
you need to do something to your model and/or service layer when
the<br>
service returns, map commands in the context for the custom
event"</p>
<p>I am having a service called "XMLLoader" . How can i map
XMLLoader.Complete to a command "InitAppCommand" ? It does not look
sensible because<br>
=> XMLLoader service can be used many times in my application in
future. And if i map it to InitAppCommand ( which is intended to
run only once, on app startup), it will call the command again and
again ( whenever used )</p>
<p>( I am just trying to follow the traditional method. Can't take
risk of using detain() or something new :) )</p></div>vishwas.gagranitag:robotlegs.tenderapp.com,2009-10-18:Comment/207176462012-11-12T17:47:10Z2012-11-12T17:47:10ZNo subject<div><p>Well, I'm having to infer a lot about the structure of your app,
so<br>
obviously my responses have limitations.</p>
<p>It looks like your service does multiple things so I would split
it up so<br>
the separation is more defined. It looks like you have a
service.execute()<br>
method that is used for multiple operations. If one of those is
a<br>
one-time-only setup task, I would have a separate method on the
service<br>
just for that operation (or, if you really want separation, make a
separate<br>
service class for each operation). Have one custom event that the
service<br>
class dispatches when the "setup" operation is complete, and others
that<br>
are dispatched when each other type of operation completes.</p>
<p>Also, it is possible to set up a command mapping that's only
fired once, by<br>
setting the fourth parameter ("oneShot") of commandMap.mapEvent()
to true.<br>
I like to do that for all my startup commands so they automatically
get<br>
unmapped once they've fired.</p>
<p>Paul</p></div>Paul Robertsontag:robotlegs.tenderapp.com,2009-10-18:Comment/207176462012-11-12T18:23:25Z2012-11-12T18:23:25ZNo subject<div><p>hmm...<br>
I think, it's good to go for the commandMap.mapEvent()'s 4th
parameter.</p>
<p>Thankyou.<br>
Vishwas</p></div>vishwas.gagrani