cancel
Showing results for 
Search instead for 
Did you mean: 

What to do in onExit and destroy function ?

MioYasutake
Active Contributor
0 Kudos

Hi expoerts,

This is a generic kind of question.

In SAPUI5, we have onExit function in controller and destroy function in Component.js.

To confess, I have never used these functions.

What do you gnerally do in these functions, and what kind of negative effect would you expect if you didn't do that?

I appreciate your opinions!

Regards, Mio

Accepted Solutions (1)

Accepted Solutions (1)

maheshpalavalli
Active Contributor

To be frank you don't need to write anything here and any application should work fine without implementing them (most cases).

When you implement the destroy function, it will be triggered when that particular control/class is destroyed, here we usually destroy controls that we have created and are not bound to the view, usually I do this in custom libraries. Because if a control is bound to a view (addDependent) then the control get's automatically destroyed. Incase if it is not bound to any control, then we can destroy them manually there, if not, the dom will still be intact and worst case, if you are using hardcoded "ID" for a control, then it will give error again(creating the control with same id will give error if the control is not destroyed) when you come back again inside the ui5 app from launpad(without refreshing the browser).

Similarly like above "onExit" hook method is also used, but that method is triggered when you are coming out of a application( from fiori app to launchpad) or closing the browser(which will be not cause issue incase of harcoded "ID") but navigating out of application to launchpad and coming back again will cause issue that harcoded ID issue, so here also we usually destroy stuff.

https://answers.sap.com/questions/11680205/onexit-best-practices-in-sap-ui5.html

-Mahesh

MioYasutake
Active Contributor
0 Kudos

Hi Mahesh,

Thanks for your prompt response!

Understood that controls can remain in DOM if it's not bound to the view.

By the way, I found onExit being used for detaching Routing event listeners.

https://sapui5.hana.ondemand.com/#/topic/7f651316d396400d88835adb0d021532

Do we need to care about attached events when leaving an app? (Do you ever do this?)

Regards,

Yasutake

maheshpalavalli
Active Contributor
0 Kudos

Hi mioyasutake,

I am also not sure, I guess it's because they are not destroying the component in the component.js like below:

	destroy: function () {
			this._oErrorHandler.destroy();
			// call the base component's destroy function
			UIComponent.prototype.destroy.apply(this, arguments);
		},

The above wil destroy the router also I believe and as the above code is not there in component.js, they probably are destroying it manually in all the views. (Just my assumption, but not sure completely)

-Mahesh

MioYasutake
Active Contributor

Thanks Mahesh,

Understood your point. In the demo app, Component.js doesn't destory itsself.

Answers (0)