SAPUI5-Which method to call once a view is displayed everytime?

Papu picture Papu · Feb 23, 2014 · Viewed 49.7k times · Source

I want to do some logic before a view is displayed every time.I cannot go with before rendering method since it is called only once when the view is created and shown for the first-time.Pls help.Any ideas?

Answer

Jason Scott picture Jason Scott · Feb 23, 2014

Why do you think the before rendering method of a control is called only once? Do you have any sample code?

I've created a quick a dirty example on jsbin (http://jsbin.com/qikokayo/1/edit?html,output) for you to have a look at. It clearly shows that onInit is called once but onBeforeRendering is called every time...

Also see this snippet from the OpenUI5 SDK which explains:

Lifecycle Hooks

onInit() - Called when a View is instantiated and its controls (if available) are already created. Can be used to modify the View before it is displayed to bind event handlers and do other one-time initialization.

onExit() - Called when the View is destroyed. Use this one to free resources and finalize activities.

onAfterRendering() - Called when the View has been rendered (therefore its HTML is part of the document). Post-rendering manipulations of the HTML can be done here. This hook is the same one that SAPUI5 controls get after being rendered.

onBeforeRendering() - Is invoked before the Controller's View is re-rendered. You would use onInit() in the case that the hook shall be invoked only before the first rendering.

For Controllers without a View, no lifecycle hooks will be called.