tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/721-best-way-for-service-result-dispatch-event-or-update-modelRobotlegs: Discussion 2011-12-31T09:45:17Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/114863002011-11-17T13:18:33Z2011-11-18T09:18:37ZBest way for Service result: dispatch event or update model?<div><p>Hi,</p>
<p>I'm new to RL and never used other frameworks as well. Please
help me to figure out one question.<br>
Best practices tells that service should dispatch an event on
service result. And as I understood, mapped command to that event
will update the model.<br>
If I have e.g. user profile loading service in my app, I need to
create several events:<br>
1. User profile request event, for view mediator (when view will
need that profile)<br>
2. Profile load success event (for service when profile will be
loaded)<br>
3. And profile load error event<br>
Also I need:<br>
4. Command for profile request, that will execute service API<br>
5. Command for successful profile load, that will update the model
with new profile</p>
<p>My question: is that the best practice for MVCS? Or I can reduce
number of classes with:<br>
1. Update model directly from service response handler<br>
2. Don't use custom events but use flash.events.Event with custom
type.<br>
I think it's a lot of events, if I have 100 small services in
couple lines of code each, I still need 300 events and 200 commands
classes.</p>
<p>Thanks in advance!</p></div>a.ptag:robotlegs.tenderapp.com,2009-10-18:Comment/114863002011-11-18T15:45:15Z2011-11-18T15:45:15ZBest way for Service result: dispatch event or update model?<div><p>Hi a.p.,</p>
<ul>
<li>Yes, if you want/need to take a short-cut, you can update your
Model from your Service class, and let the Model dispatch an event
to the rest of your application with the updated data as a payload.
Mediators can listen for that event and pass the data to their
Views. Actors(Models and Services), Mediators and Commands are
communicating with each other through the shared eventDispatcher
provided by the Context. Models and Services can only dispatch
events.</li>
</ul>
<p>In the following example SomeService is updating SomeModel after
receiving the data from an external service. SomeModel dispatches
an event, SomeMediator listens to it and passes the payload to its
view (SomeView)</p>
<pre>
<code>
public class SomeModelEvent extends Event
{
public static const DATA_UPDATED:String="dataUpdated";
public function SomeModelEvent(type:String, someUpdatedData:ArrayCollection, bubbles:Boolean=false, cancelable:Boolean=false)
{
_someUpdatedData=someUpdatedData;
super(type, bubbles, cancelable);
}
protected var _someUpdatedData:ArrayCollection;
override public function clone():Event
{
return new SomeModelEvent(type, someUpdatedData, bubbles, cancelable);
}
public function get someUpdatedData():ArrayCollection
{
return _someUpdatedData;
}
}</code>
</pre>
<pre>
<code>
public class SomeModel extends Actor
{
protected var _someData:ArrayCollection;
public function get someData():ArrayCollection
{
return _someData;
}
public function set someData(value:ArrayCollection):void
{
_someData = value;
dispatch(new SomeModelEvent(SomeModelEvent.DATA_UPDATED, someData));
}
}</code>
</pre>
<pre>
<code>
public class SomeService extends Actor implements ISomeService
{
[Inject]
public var someModel:SomeModel;
private function onDataReceived(someResult:Object):void
{
someModel.someData= someResult;
}
}</code>
</pre>
<pre>
<code>
public class SomeMediator extends Mediator
{
[Inject]
public var view:SomeView;
override public function onRegister():void
{
eventMap.mapListener(eventDispatcher, SomeModelEvent.DATA_UPDATED, onDataUpdated);
}
protected function onDataUpdated(event:SomeModelEvent):void
{
view.setListDataProvider(event.someUpdatedData);
}
}</code>
</pre>
<p>SomeView.mxml</p>
<pre>
<code>
import mx.collections.ArrayCollection;
public function setListDataProvider(dataProvider:ArrayCollection):void
{
someViewList.dataProvider=dataProvider;
}</code>
</pre>
<ul>
<li>Regarding the number of events <strong>my personal
preference</strong> is to have a custom event like this:</li>
</ul>
<p>public class AuthorEvent extends Event<br>
{</p>
<p>public static const AUTHOR_DELETED:String = "authorDeleted";<br>
public static const AUTHOR_SAVED:String = "authorSaved";<br>
public static const AUTHOR_UPDATED:String = "authorUpdated";<br>
...</p>
<p>having different event types for different actions within an
event class for everything related to Author.</p>
<p>or</p>
<p>something more generic, used by different actors:</p>
<p>public class CRUDEvent extends Event //bad name, but it’s
just an example:)<br>
{</p>
<p>public static const ITEM_DELETED:String = "itemDeleted";<br>
public static const ITEM_SAVED:String = "itemSaved";<br>
public static const ITEM_UPDATED:String = "itemUpdated";<br>
...</p>
<p>I hope this helps. Don’t hesitate to ask more questions
:)<br>
Ondina</p></div>Ondina D.F.