Best way for Service result: dispatch event or update model?

a.p's Avatar

a.p

17 Nov, 2011 01:18 PM

Hi,

I'm new to RL and never used other frameworks as well. Please help me to figure out one question.
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.
If I have e.g. user profile loading service in my app, I need to create several events:
1. User profile request event, for view mediator (when view will need that profile)
2. Profile load success event (for service when profile will be loaded)
3. And profile load error event
Also I need:
4. Command for profile request, that will execute service API
5. Command for successful profile load, that will update the model with new profile

My question: is that the best practice for MVCS? Or I can reduce number of classes with:
1. Update model directly from service response handler
2. Don't use custom events but use flash.events.Event with custom type.
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.

Thanks in advance!

  1. Support Staff 1 Posted by Ondina D.F. on 18 Nov, 2011 03:45 PM

    Ondina D.F.'s Avatar

    Hi a.p.,

    • 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.

    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)

    
    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;
            }
        }
    
    
    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));
            }
        }
    
    
    public class SomeService extends Actor implements ISomeService
    {
    [Inject]
     public var someModel:SomeModel;
    
    
            private function onDataReceived(someResult:Object):void
            {
                
                someModel.someData= someResult;
            }
    }
    
    
    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);
            }
        }
    

    SomeView.mxml

    
    import mx.collections.ArrayCollection;
    
                public function setListDataProvider(dataProvider:ArrayCollection):void
                {
                    someViewList.dataProvider=dataProvider;
                }
    
    • Regarding the number of events my personal preference is to have a custom event like this:

    public class AuthorEvent extends Event
    {

    public static const AUTHOR_DELETED:String = "authorDeleted";
    public static const AUTHOR_SAVED:String = "authorSaved";
    public static const AUTHOR_UPDATED:String = "authorUpdated";
    ...

    having different event types for different actions within an event class for everything related to Author.

    or

    something more generic, used by different actors:

    public class CRUDEvent extends Event //bad name, but it’s just an example:)
    {

    public static const ITEM_DELETED:String = "itemDeleted";
    public static const ITEM_SAVED:String = "itemSaved";
    public static const ITEM_UPDATED:String = "itemUpdated";
    ...

    I hope this helps. Don’t hesitate to ask more questions :)
    Ondina

  2. Ondina D.F. closed this discussion on 31 Dec, 2011 09:45 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac