Hooks are extension points in the controller code that are used to make controller extensions more stable.
The controller extension concept enables you to override any method. This is a powerful but also fragile feature. Extension points, so-called hooks, can be provided in the controller code. These hooks can be documented and kept stable, thus providing more robust hooks across application updates for controller extensions.
The process for this is as follows:
By receiving the data object oSomeData from the server, the application enables you to access and modify the data object. The extension function name is onDataReceived and gets a reference to the data object as argument.
Code of the standard controller:
#!js // ...data object oSomeData has been received, possibly from an Ajax response... if (this.onDataReceived) { // check whether any extension has implemented the hook... this.onDataReceived(oSomeData); // ...and call it } // ...continue working with the (now possibly modified) data...
Code of the custom controller:
#!js sap.ui.controller("customer.xy.Sub2ControllerExtension", { onDataReceived: function(oData){ // oSomeData will be passed in if (oData && oData.status === "important") { oData.message = oData.message + "!!!"; // modify some part of the data object, adding exclamation marks to a message text } } // no need to return anything as in this example the original object is modified });
This only works for one extension layer as the most specific or last extension overrides any other hook implementations. To allow multi-layer extensions, we recommend that middle-layer extensions provide and document their own hook functions.
This also requires flat, non-inherited controllers defined with the sap.ui.controller(...) function used as extension controller, and not with typed controllers.