A Multiton 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 Multiton, so you should not call the constructor directly, but instead call the static Factory method, passing the unique key for this instance Controller.getInstance(multitonKey)

    Parameters

    • key: string

    Returns Controller

    Error if instance for this Multiton key has already been constructed

Properties

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

Mapping of Notification names to Command factories

multitonKey: string

The Multiton Key for this Core

view?: IView

Local reference to View

instanceMap: {
    [key: string]: IController;
} = {}

Multiton Instances

MULTITON_MSG: string = "Controller instance for this Multiton key 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 Multiton 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("ViewTestKey1", (key: string) => new View(key));
    }

    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 Multiton Factory method.

    Parameters

    • key: string

      The key used to identify the controller instance.

    • factory: ((key: string) => IController)

      A factory function that creates a new instance of the controller if one does not already exist for the specified key.

    Returns IController

    The controller instance associated with the given key.

  • Remove a Controller instance

    Parameters

    • key: string

      The key used to identify the controller instance to be removed.

    Returns void