Extending Apps in SAPUI5 - Controller Hooks

In view, there are extension points, similarly in controllers, there are hooks. 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.

Let me just explain the steps for providing hooks in the controller:

1) Identify a position, where you want to plug in and execute their customized code.
2) In the application, define a new function name which is reserved for the extension, document the function and any arguments the function may receive or return.
3) Add code lines in the application (see code snippet below) to check whether the function has been implemented, and, if so, to call the function.
4) You can then configure a controller extension, implementing exactly this one function.
5) SAPUI5 runtime merges the new controller extension into the standard controller. If customizing is enabled, the new function can be executed.

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:
// ...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:
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

Controller Replacement
Standard controller can be replaced by specifying a new controller name in a replacement View and implementing this Controller.
For a view replacement, you can either use the standard controller of the replaced view by setting its name as controllerName, or use or extend the standard controller, or you can replace the controller by specifying a new controller name in the new view and implementing the new controller.

Follow my next blog for an example on extending app using SAP Web IDE.
Next Post »