PureMVC AS3 Framework - [Standard Version] | All Packages | All Classes | Index | Frames No Frames |
Interface IMediator

Package | org.puremvc.as3.interfaces
Interface | public interface IMediator
Implementors | Mediator

The interface definition for a PureMVC Mediator.

In PureMVC, `IMediator` implementors assume these responsibilities:

- Implement a common method which returns a list of all `INotification`s the `IMediator` has interest in.
- Implement a notification callback method.
- Implement methods that are called when the IMediator is registered or removed from the View.

Additionally, `IMediator`s typically:

- Act as an intermediary between one or more view components such as text boxes or list controls, maintaining references and coordinating their behavior.
- In Flash-based apps, this is often the place where event listeners are added to view components, and their handlers implemented.
- Respond to and generate `INotifications`, interacting with of the rest of the PureMVC app.

When an `IMediator` is registered with the `IView`, the `IView` will call the `IMediator`'s `listNotificationInterests` method. The `IMediator` will return an `Array` of `INotification` names which it wishes to be notified about.

The `IView` will then create an `Observer` object encapsulating that `IMediator`'s (`handleNotification`) method and register it as an Observer for each `INotification` name returned by `listNotificationInterests`.

A concrete IMediator implementor usually looks something like this:

      import org.puremvc.as3.patterns.mediator.*;
      import org.puremvc.as3.patterns.observer.*;
      import org.puremvc.as3.core.view.*;

      import com.me.myapp.model.*;
      import com.me.myapp.view.*;
      import com.me.myapp.controller.*;

      import mx.controls.ComboBox;
      import mx.events.ListEvent;

      public class MyMediator extends Mediator implements IMediator {

        public function MyComboMediator( viewComponent:Object ) {
         super( viewComponent );
         combo.addEventListener( Event.CHANGE, onChange );
        }

        override public function listNotificationInterests():Array {
          return [ MyFacade.SET_SELECTION,
             MyFacade.SET_DATAPROVIDER ];
        }

        override public function handleNotification( notification:INotification ):void {
          switch ( notification.getName() ) {
           case MyFacade.SET_SELECTION:
            setSelection(notification);
            break;
           case MyFacade.SET_DATAPROVIDER:
            setDataProvider(notification);
            break;
          }
        }

        // Set the data provider of the combo box
        protected function setDataProvider( notification:INotification ):void {
         combo.dataProvider = notification.getBody() as Array;
        }

        // Invoked when the combo box dispatches a change event, we send a
           // notification with the
        protected function onChange(event:ListEvent):void {
         sendNotification( MyFacade.MYCOMBO_CHANGED, this );
        }

        // A private getter for accessing the view object by class
           protected function get combo():ComboBox  {
              return view as ComboBox;
           }

      }

See also

INotification

Public Methods

| Method | Defined by
--- | --- | ---
|  | getMediatorName():String Get the `IMediator` instance name | IMediator
|  | getViewComponent():Object Get the `IMediator`'s view component. | IMediator
|  | handleNotification(notification:INotification):void Handle an `INotification`. | IMediator
|  | listNotificationInterests():Array List `INotification` interests. | IMediator
|  | onRegister():void Called by the View when the Mediator is registered | IMediator
|  | onRemove():void Called by the View when the Mediator is removed | IMediator
|  | setViewComponent(viewComponent:Object):void Set the `IMediator`'s view component. | IMediator

Method detail

getMediatorName | () | method

`public function getMediatorName():String`

Get the `IMediator` instance name

Returns

| `String` — the `IMediator` instance name

getViewComponent | () | method |

`public function getViewComponent():Object`

Get the `IMediator`'s view component.

Returns

| `Object` — Object the view component

handleNotification | () | method |

`public function handleNotification(notification:INotification):void`

Handle an `INotification`.

Parameters

| `notification:INotification` — the `INotification` to be handled

listNotificationInterests | () | method |

`public function listNotificationInterests():Array`

List `INotification` interests.

Returns

| `Array` — an `Array` of the `INotification` names this `IMediator` has an interest in.

onRegister | () | method |

`public function onRegister():void`

Called by the View when the Mediator is registered

onRemove | () | method |

`public function onRemove():void`

Called by the View when the Mediator is removed

setViewComponent | () | method |

`public function setViewComponent(viewComponent:Object):void`

Set the `IMediator`'s view component.

Parameters

| `viewComponent:Object` — the view component