Can someone give a more detailed explanation about the lifecycle of the default events of a UI5 Control? I know there is this page on the documentation that gives an overview of a Control lifecycle, however, I think it is very brief and wanted something more detailed. Can someone list the order of the events of a Control and explain what every event does?
You are absolutely right. The details of a Control lifecycle and implementation details are very well hidden in the docs. I'll try to sum up my so far understanding for you.
The lifecycle of a Control is mainly determined by:
Here is a sample implementation with some sample usages for the different hooks:
sap.ui.core.Control.extend("a.sample.Control", {
init : function() {
// instantiate a sub-control
this._btn = new sap.m.Button();
},
onBeforeRendering : function() {
// deregister a listener via jQuery
this.$("subelement").off("click", this.subElementClick);
},
onAfterRendering : function() {
// register a listener via jQuery on a sub-element
this.$("subelement").on("click", this.subElementClick);
},
subElementClick : function() {
// do stuff
},
exit : function() {
// clean up sub-controls and local references
this._btn.destroy();
delete this._btn;
}
});
Why shouldn't I do my init stuff in my constructor?
There is a basic UI5 constructor in ManagedObject. It "prepares" your UI5 object for you and calls your init function afterwards. That means in your init all settings will already be applied for you and you can access properties and aggregations as usual.
Why shouldn't I call rerender?
The SAPUI5 rendering is intelligent in a sense that it groups and optimizes queued rerenderings. Therefore you should never call rerender
directly but instead use invalidate
to mark a control for rerendering.
HF
Chris