cancel
Showing results for 
Search instead for 
Did you mean: 

usage of onBeforeRendering vs onafterRendering

Former Member
0 Kudos

Hi All,

Can someone explain usage of onBeforeRendering vs onafterRendering with some example.

I have googled but did not find any apt answer.

Thanks,

John

Accepted Solutions (1)

Accepted Solutions (1)

Hello Jhon,

Yes you got it right.

As far as onAfterRendering is concerned this will trigger once all the control in the view has been rendered.If you want to perform some action on any control then you have to do it in onafterrendering.For example you have  table control in your view and you have binded it with model if you want to add more column in table at runtime then you have to do it in onafterrendering or if you want to hide some control on the basis on something then you have to do it in onafterrendering because onafterrendering will trigger when all the controls will be loaded in the view.

Hope it helps.

Thanks and Regards,

Umer Siddique

Former Member
0 Kudos

Hello Umar,

Sorry for late reply.

Again if we want to hide any control we can read model data in beforeredering and can do validations as per our need. There could some else purpose for afterrendering.

Thanks,

John

0 Kudos

Hi Jhon,

i just told you the one usage of onafterrendering to clarify your thoughts on  onBeforeRendering and onafterRendering and yes onafterRendering is used for many other purposes also.As far as you said we can read model and do validations in onBeforeRendering let me clarify you more that yes you can read model (as it has nothing to do with rendering) but no validation on controls can be performed in onbeforerendering because when onbeforerendering method is triggered the controls are not even rendered so if you try to access any control  using sap.ui.getCore().byId('abcd') then it will give you error that no id found this is because controls are not yet rendered.

so it can only be done in onafterrendering because onafterrendering will only triggered once all the controls are rendered and then you can do anything with control( i.e,doing any kind of validations,changing properties of any control  ,hiding any control etc).

Hope it helps.

Thanks and Regards,

Umer Siddique

Former Member
0 Kudos

Convinced...:)

Thanks,

John

0 Kudos

Nice explanation

Answers (2)

Answers (2)

0 Kudos

Hello Jhon,

Make two views and put breakpoint in all methods and you will see all the onint breakpoint will be triggered first and in your case on button click onbeforerender breakpoint will not be triggered because you are just clicking on the button but view itself is not re rendering .Make two  views for navigation (from one view to another and back to previous view) then you will see onint will only be triggered once not again and again and don't forget to invalidate (by using this sap,ui.getCore().byId("view2").invalidate() ) the view before going to next view otherwise both views breakpoint will only be triggered once not again and again.

Hope it helps.

Thanks and Regards,

Umer Siddique

Former Member
0 Kudos

Hi Umer,

You are saying oninit will trigger only once when view is loading first time, if we do navigate to other view come back same view this time oninit will not trigger. - Correct me if im wrong.

how about OnAfterRendering - can you explain bit more or taking any another scenario the usage of this function.

Thanks,

John

0 Kudos

Hello Jhon,

onAfterRendering is called when the view has been rendered and, therefore, its HTML is part of the document; used to do post-rendering manipulations of the HTML. OpenUI5 controls get this hook after being rendered while  onBeforeRendering Invoked before the controller view is re-rendered and not before the first rendering; use onInit for invoking the hook before the first rendering.


OnBeforeRendering is used when you want to perform something before the view is loaded lets say you want to load oData model before the view is loaded.


OnAfterRendering is used when you want to perform some activity After the view is loaded let say you want to want to add extra row in table after rendering of table for this OnAfterRendering method will be used becouse all the controls will be rendered after this.


Thanks and Regards,

Umer Siddique

Former Member
0 Kudos

Hello Umar,

Thanks for your reply.

OnBeforeRendering is used when you want to perform something before the view is loaded

we can use oninit also to acheive this why OnBeforeRendering  only.


OnAfterRendering - can you explain bit more or taking any another scenario.



Thanks,

John

0 Kudos

Hello Jhon,

Yes you are right jhon you can do it in onint, but the main difference between both the methods is that onint is only called first time when the view is rendering.it will not be called again and again whenever the view is re rendered again.That is why  onint is mostly used to initialize some parameters  .

While onbeforerendering will be called whenever the view is re rendered again.



Lets say you want to get  data from OData model everytime you load your view so that you can show user with the real time data.But if you did this in onint then user will not be able to see the changes that are made after the first call of the service(When the view is first time rendered) doesn't matter how many time view is re rendered as onint will not be called again and again.Thats why we do this in onbeforerendering.


Hope it helps.

Thanks and Regards,

Umer Siddique

.

Former Member
0 Kudos

Hello Umar,

I took the example.

I have created a view with only one button and in controller class all methods i put breakpoints. I started debugging first-oninit, second-beforerender, third-afterrender got triggered.

Once view is loaded completely I clicked the button the result no breakpoint is triggered. As you said in this case beforerender breakpoint should trigger right cause some rendering is happened.

I think this eg clears my question.

Thanks,

John