Sunday, December 18, 2016

Visualforce - Beyond Basics Series 24 - lazy load the page

What is Lazy Load ?

Loading different contents of the page after the initial page load is called Lazy loading. This is contrary to most common method called Eager Loading.


Why should i care about ?

When a developer starts to build a page for a Org, they might have few requirements. But, as days pass on, they may get more requirements from customers & keep adding features one by one. Most of the time, Controller needs to do heavy lifting during its construction, that means it will have more logic placed inside constructor. This will make page loading time way high & gives very bad performance.

Where it will be useful ?

When you want to make Heavy DML calls involving huge number of records or having complex Apex processing, then, it's bad idea to have that logic in constructor.

How to do it ?

Ideally, Constructor should be used to initialize the objects to bare minimum, that means just initializing to empty object. In another view, it must have only logic to display very minimum components to the user, other stuff needs to be handled through separate action functions.
For example, if you are displaying Table of data, its better to use pagination with small number of records set number. If you are displaying record details, you can display overview information & fetch more details if user press for 'more details' button/link.


Where is the code ?

I have illustrated a example, where i need to fetch list of closed opportunity number. This is very intensive Soql call, as it needs to look through all records in the Org. If i do that in Constructor, it would take few seconds to load the  page. Since, i did in action function, user will wait knowing that why its taking more time(by looking at the loading status message).


No comments:

Post a Comment

Thanks for reading my blog !