A Singleton Controller implementation.

In PureMVC, the Controller class follows the 'Command and Controller' strategy, and assumes these responsibilities:

  • Remembering which Commands are intended to handle which Notifications.
  • Registering itself as an Observer with the View for each Notification that it has a Command mapping for.
  • Creating a new instance of the proper Command to handle a given Notification when notified by the View.
  • Calling the Command's execute method, passing in the Notification.

Your application must register Commands with the Controller.

The simplest way is to subclass Facade, and use its initializeController method to add your registrations.

Implements

Constructors

  • Constructor.

    This Controller implementation is a Singleton, so you should not call the constructor directly, but instead call the static Singleton Factory method, Controller.getInstance()

    Returns Controller

    Error if instance for this Singleton has already been constructed

Properties

commandMap: {
    [key: string]: (() => ICommand);
}

Mapping of Notification names to Command factories

view?: IView

Local reference to View

instance: IController

Singleton instance

SINGLETON_MSG: string = "Controller Singleton already constructed!"

Message Constants

Methods

  • If a Command has previously been registered to handle the given Notification, then it is executed.

    Parameters

    • notification: INotification

      The notification containing the data or command details needed for execution.

    Returns void

  • Check if a Command is registered for a given Notification

    Parameters

    • notificationName: string

      The name of the notification to check for a registered command.

    Returns boolean

    true if a command is registered for the specified notification name; otherwise, false.

  • Initialize the Singleton Controller instance.

    Called automatically by the constructor.

    Note that if you are using a subclass of View in your application, you should also subclass Controller and override the initializeController method in the following way:

    // ensure that the Controller is talking to my View implementation
    initializeController() {
    this.view = MyView.getInstance(() => new View());
    }

    Returns void

  • Register a particular Command class as the handler for a particular Notification.

    If an Command has already been registered to handle Notifications with this name, it is no longer used, the new Command is used instead.

    The Observer for the new Command is only created if this the first time a Command has been registered for this Notification name.

    Parameters

    • notificationName: string

      The name of the notification to associate with the command.

    • factory: (() => ICommand)

      A factory function that returns an instance of the command.

    Returns void

  • Remove a previously registered Command to Notification mapping.

    Parameters

    • notificationName: string

      The name of the notification for which the associated command should be removed.

    Returns void

  • Controller Singleton Factory method.

    Parameters

    Returns IController

    the Singleton instance of Controller.