Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Welcome, Guest. Please login or register.
April 27, 2017, 05:24:49 PM
Home Help Search Login Register
News: ATTENTION: Spambots must die! Humans must visit http://contact.futurescale.com to request forum access.
Pages: [1]
Print
Author Topic: Command dispatcher  (Read 2707 times)
lulumOriss
Jr. Member
**
Posts: 14


View Profile
« on: December 07, 2011, 07:57:43 AM »

I often find myself having to manage a system of adding and editing data from the same interface. I have not found the right way to do it yet.
My first idea was to create a command that will check what other command has to be executed from a value in the body of the notification. It was, I think, the role of a macrocommand but it is not possible to override the execute method to retrieve the notification.

What is the proper way to proceed?

Thank you. lulu.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2865



View Profile WWW
« Reply #1 on: December 07, 2011, 09:04:11 AM »

No pre-processing of the note is necessary if you make the user's intent clear through the event you send.

Send a different event from the view when you're adding than from when you are editing. Have the mediator listen for those events and send a different notification based on the event that was dispatched from the view. Then, an EditCommand or AddCommand will be triggered based on the notification that was sent from the mediator.

The mediator does the translation of the user's intent into the notification that triggers correct logic.

Code:
   /**
     * Called when mediator is registered.
     */
    override public function onRegister():void
    {
     // Set component event listeners
     form.addEventListener( AppEvent.ADD_THIS,     handleAppEvent );
     form.addEventListener( AppEvent.ADD_THAT,    handleAppEvent );
     form.addEventListener( AppEvent.EDIT_THIS,    handleAppEvent );
     form.addEventListener( AppEvent.EDIT_THAT,   handleAppEvent );
     form.addEventListener( AppEvent.DELETE_THIS,  handleAppEvent );
     form.addEventListener( AppEvent.DELETE_THAT, handleAppEvent );
    }
    
    /**
     * Handle AppEvents from the view component.
     */
    private function handleAppEvent( event:AppEvent ):void
    {
     switch ( event.type )
     {
        case AppEvent.ADD_THIS:
         sendNotification( AppConstants.ADD_THIS, event.data );
         break;
        
        case AppEvent.ADD_THAT:
         sendNotification( AppConstants.ADD_THAT, event.data );
         break;
        
        case AppEvent.EDIT_THIS:
         sendNotification( AppConstants.EDIT_THIS, event.data );
         break;
        
        case AppEvent.EDIT_THAT:
         sendNotification( AppConstants.EDIT_THAT, event.data );
         break;        

        // Sometimes multiple events need to trigger the same command...
        case AppEvent.DELETE_THIS:
        case AppEvent.DELETE_THAT:
         sendNotification( AppConstants.DELETE_ITEM, event.data );
         break;
        
     }
    }

-=Cliff>
Logged
lulumOriss
Jr. Member
**
Posts: 14


View Profile
« Reply #2 on: December 07, 2011, 09:25:18 AM »

Hi Cliff,

Thank you for your answer. That's make sense.

lulu.
Logged
Pages: [1]
Print
Jump to: