Dart DocumentationpuremvcController

Controller Class

A PureMVC MultiCore IController implementation.

In PureMVC, an IController implementor follows the 'Command and Controller' strategy, and assumes these responsibilities:

See INotification, ICommand

Implements

IController

Constructors

Code new Controller(String key) #

Constructor.

This IController implementation is a Multiton, so you should not call the constructor directly, but instead call the static getInstance method.

Controller( String key )
{
  if ( instanceMap[ key ] != null ) throw new MultitonErrorControllerExists();
  multitonKey = key;
  instanceMap[ multitonKey ] = this;
  commandMap = new Map<String,Function>();    
  initializeController();    
}

Static Methods

Code IController getInstance(String key) #

IController Multiton Factory method.

  • Returns the IController Multiton instance for the specified key
static IController getInstance( String key )
{
  if ( key == null || key == "" ) return null;
  if ( instanceMap == null ) instanceMap = new Map<String,IController>();
  if ( instanceMap[ key ] == null ) instanceMap[ key ] = new Controller( key );
  return instanceMap[ key ];
}

Code void removeController(String key) #

Remove an IController instance.

  • Param key multitonKey of the IController instance to remove
static void removeController( String key )
{
  instanceMap[ key ] = null;
}

Static Fields

Code Map<String, IController> instanceMap #

static Map<String,IController> instanceMap;

Methods

Code void executeCommand(INotification note) #

Execute the ICommand previously registered as the handler for INotifications with the given notification's name.

void executeCommand( INotification note )
{
  Function commandFactory = commandMap[ note.getName() ];
  if ( commandFactory == null ) return;

  ICommand commandInstance = commandFactory();
  commandInstance.initializeNotifier( multitonKey );
  commandInstance.execute( note );
}

Code bool hasCommand(String noteName) #

Check if an ICommand is registered for a given INotification name with the IController.

  • Param noteName - the name of the INotification.
  • Returns bool - whether an ICommand is currently registered for the given noteName.
bool hasCommand( String noteName )
{
  return commandMap[ noteName ] != null;
}

Code void initializeController() #

Initialize the IController Multiton instance.

Called automatically by the constructor.

Note that if you are using a custom IView implementor in your application, you should also subclass Controller and override the initializeController method, setting view equal to the return value of a call to getInstance on your IView implementor.

void initializeController( )
{
  view = View.getInstance( multitonKey );
}

Code void registerCommand(String noteName, Function commandFactory) #

Register an INotification to ICommand mapping with the Controller.

  • Param noteName - the name of the INotification to associate the ICommand with.
  • Param commandFactory - a function that creates a new instance of the ICommand.
void registerCommand( String noteName, Function commandFactory )
{
  if ( commandMap[ noteName ] == null ) {
    view.registerObserver( noteName, new Observer( executeCommand, this ) );
  }
  commandMap[ noteName ] = commandFactory;
}

Code void removeCommand(String noteName) #

Remove a previously registered INotification to ICommand mapping from the IController.

void removeCommand( String noteName )
{
  // if the Command is registered...
  if ( hasCommand( noteName ) )
  {
    // remove the observer
    view.removeObserver( noteName, this );
                
    // remove the command
    commandMap[ noteName ] = null;
  }
}

Fields

Code Map<String, Function> commandMap #

Map<String,Function> commandMap;

Code String multitonKey #

String multitonKey;

Code IView view #

IView view;