I'm trying to create a base controller that has common methods and common onInit
logic.
Using the extend
method adds the methods from the base controller to the child controller, but the lifecycle methods get overwritten completely.
- I tried using the
override.onInit
approach shown on this documentation page but it did not work. - I also tried with
sap.ui.component
like this but I couldn't get it to work at all.
I'm not sure if it should work with the AMD syntax.
As far as I understood, the extension feature should replace common methods that have been overridden, but it should execute the lifecycle methods from both the base and extension controller on this respective order.
So my question are the following:
- Is this behavior possible to achieve? If so, what am I doing wrong?
- Is there a better way to implement it?
Example code:
Parent controller
sap.ui.define([
"sap/ui/core/mvc/Controller"
], function (Controller) {
"use strict";
return Controller.extend("my.namespace.controller.App", {
onInit: function () {
console.log("reusable init");
},
// ...
});
});
Child controller
sap.ui.define([
"my/namespace/controller/App.controller"
], function (Controller) {
"use strict";
return Controller.extend("my.namespace.child.controller.App", {
onInit: function () {
console.log("extend init");
},
// ...
});
});
This example logs only "extend init" when I run the application. I expected it to log both "reusable init" and "extend init" in this order.
I've omitted some code, but the main idea is represented on these two controllers. The only relevant metadata is that the base controller is abstract.
extend
API in UI5. In that case, the classical BaseController approach would be appropriate. But if you want to let UI5 call the (lifecycle) methods, then you'd have to use one of the extension concepts.