Class sap.ui.core.mvc.ControllerModule: sap/ui/core/mvc/Controller


A generic controller implementation for the UI5 Model-View-Controller concept.

Can either be used as a generic controller which is enriched on the fly with methods and properties (see sap.ui.controller) or as a base class for typed controllers.

Constructor Summary
new sap.ui.core.mvc.Controller(sName)Instantiates a (MVC-style) controller.
Method Summary
sap.ui.core.mvc.Controller.extend(sClassName, oClassInfo?, FNMetaImpl?)Creates a new subclass of class sap.ui.core.mvc.Controller with name sClassName and enriches it with the information contained in oClassInfo.
sap.ui.core.mvc.Controller.getMetadata()Returns a metadata object for class sap.ui.core.mvc.Controller.
sap.ui.core.mvc.Controller.registerExtensionProvider(sExtensionProvider)Registers a callback module, which provides code enhancements for the lifecycle and event handler functions of a specific controller.
byId(sId)Returns an Element of the connected view with the given local ID.
createId(sId)Converts a view local ID to a globally unique one by prepending the view ID.
getOwnerComponent()Gets the component of the controller's view

If there is no Component connected to the view or the view is not connected to the controller, undefined is returned.

getView()Returns the view associated with this controller or undefined.
onAfterRendering()This method is called every time the View is rendered, after the HTML is placed in the DOM-Tree.
onBeforeRendering()This method is called every time the View is rendered, before the Renderer is called and the HTML is placed in the DOM-Tree.
onExit()This method is called upon desctuction of the View.
onInit()This method is called upon initialization of the View.
Methods borrowed from class sap.ui.base.Object
Constructor Detail
new sap.ui.core.mvc.Controller(sName)
Instantiates a (MVC-style) controller. Consumers should call the constructor only in the typed controller scenario. In the generic controller use case, they should use sap.ui.controller instead.
Parameters:
{string|object[]}sName The name of the controller to instantiate. If a controller is defined as real sub-class, the "arguments" of the sub-class constructor should be given instead.
Method Detail
sap.ui.core.mvc.Controller.extend(sClassName, oClassInfo?, FNMetaImpl?): function
Creates a new subclass of class sap.ui.core.mvc.Controller with name sClassName and enriches it with the information contained in oClassInfo.

oClassInfo might contain the same kind of information as described in sap.ui.base.EventProvider.extend.

Parameters:
{string}sClassName Name of the class being created
{object}oClassInfo? Object literal with information about the class
{function}FNMetaImpl? Constructor function for the metadata object; if not given, it defaults to sap.ui.core.ElementMetadata
Returns:
{function} Created class / constructor function
sap.ui.core.mvc.Controller.getMetadata(): sap.ui.base.Metadata
Returns a metadata object for class sap.ui.core.mvc.Controller.
Returns:
{sap.ui.base.Metadata} Metadata object describing this class
sap.ui.core.mvc.Controller.registerExtensionProvider(sExtensionProvider)
Registers a callback module, which provides code enhancements for the lifecycle and event handler functions of a specific controller. The code enhancements are returned either in sync or async mode.

The extension provider module provides the getControllerExtensions function which returns either directly an array of objects or a Promise that returns an array of objects when it resolves. These objects are object literals defining the methods and properties of the controller in a similar way as sap.ui.controller.

Example for a callback module definition (sync):

sap.ui.define("my/custom/sync/ExtensionProvider", ['jquery.sap.global'], function(jQuery) {
  var ExtensionProvider = function() {};
  ExtensionProvider.prototype.getControllerExtensions = function(sControllerName, sComponentId, bAsync) {
    if (!bAsync && sControllerName == "my.own.Controller") {
      // IMPORTANT: only return extensions for a specific controller
      return [{
        onInit: function() {
          // Do something here...
        },
        onAfterRendering: function() {
          // Do something here...
        },
        onButtonClick: function(oEvent) {
          // Handle the button click event
        }
      }
    }];
  };
  return ExtensionProvider;
}, true);

Example for a callback module definition (async):

sap.ui.define("my/custom/async/ExtensionProvider", ['jquery.sap.global'], function(jQuery) {
  var ExtensionProvider = function() {};
  ExtensionProvider.prototype.getControllerExtensions = function(sControllerName, sComponentId, bAsync) {
    if (bAsync && sControllerName == "my.own.Controller") {
      // IMPORTANT:
      // only return a Promise for a specific controller since it
      // requires the View/Controller and its parents to run in async
      // mode!
      return new Promise(function(fnResolve, fnReject) {
        fnResolve([{
          onInit: function() {
            // Do something here...
          },
          onAfterRendering: function() {
            // Do something here...
          },
          onButtonClick: function(oEvent) {
            // Handle the button click event
          }
        }]);
      }
    };
  };
  return ExtensionProvider;
}, true);

The lifecycle functions onInit, onExit, onBeforeRendering and onAfterRendering are added before or after the lifecycle functions of the original controller. The event handler functions, such as onButtonClick, are replacing the original controller's function.

When using an async extension provider you need to ensure that the view is loaded in async mode.

In both cases, return undefined if no controller extension shall be applied.

Parameters:
{string}sExtensionProvider the module name of the extension provider
Since:
1.34.0
See:
sap.ui.controller for an overview of the available functions for controllers
Returns an Element of the connected view with the given local ID.

Views automatically prepend their own ID as a prefix to created Elements to make the IDs unique even in the case of multiple view instances. This method helps to find an element by its local ID only.

If no view is connected or if the view doesn't contain an element with the given local ID, undefined is returned.

Parameters:
{string}sId View-local ID
Returns:
{sap.ui.core.Element} Element by its (view local) ID
createId(sId): string
Converts a view local ID to a globally unique one by prepending the view ID.

If no view is connected, undefined is returned.

Parameters:
{string}sId View-local ID
Returns:
{string} Prefixed ID
getOwnerComponent(): sap.ui.core.Component
Gets the component of the controller's view

If there is no Component connected to the view or the view is not connected to the controller, undefined is returned.

Since:
1.23.0
Returns:
{sap.ui.core.Component} Component instance
Returns the view associated with this controller or undefined.
Returns:
{sap.ui.core.mvc.View} View connected to this controller.
onAfterRendering()
This method is called every time the View is rendered, after the HTML is placed in the DOM-Tree. It can be used to apply additional changes to the DOM after the Renderer has finished. (Even though this method is declared as "abstract", it does not need to be defined in controllers, if the method does not exist, it will simply not be called.)
See:
sap.ui.core.Control.prototype.onAfterRendering
onBeforeRendering()
This method is called every time the View is rendered, before the Renderer is called and the HTML is placed in the DOM-Tree. It can be used to perform clean-up-tasks before re-rendering. (Even though this method is declared as "abstract", it does not need to be defined in controllers, if the method does not exist, it will simply not be called.)
See:
sap.ui.core.Control.prototype.onBeforeRendering
onExit()
This method is called upon desctuction of the View. The controller should perform its internal destruction in this hook. It is only called once per View instance, unlike the onBeforeRendering and onAfterRendering hooks. (Even though this method is declared as "abstract", it does not need to be defined in controllers, if the method does not exist, it will simply not be called.)
onInit()
This method is called upon initialization of the View. The controller can perform its internal setup in this hook. It is only called once per View instance, unlike the onBeforeRendering and onAfterRendering hooks. (Even though this method is declared as "abstract", it does not need to be defined in controllers, if the method does not exist, it will simply not be called.)