Tuesday 13 March 2018

Lightning Best Practice to Access Data for Increasing performance of Application

Hi Folks,

Lightning Platform is the new buzz word. Lets talk about its best practice in a bit more detail.

My upcoming blogs will be on lightning best practice and will share my experience and findings.  Now, lets jump to main topic.


Data Access

In Lightning, accessing data is very easy as we have our super hero aura framework that does the magic. Many of the developers are stuck by the performance of the application. If not then sometimes client comes back to us and complaining about performance. 

For all these issues we have an old remedy but in new style. Word is CACHING! CACHING!. 

Ways to do Caching in lightning components:-

Caching in lightning is done by unique way . You dont have to write a full code for caching or to do something at browser level . 

action.setStorable();

({
    myAction : function(component, event, helper) {
        var action = component.get("c.getAccounts"); 
        //alert('startTime'+startTime);
        action.setStorable();
        action.setCallback(this, function(actionResult) {
            
            var state = actionResult.getState();
            if (state === "SUCCESS") {
                console.log("time taken to load in ms ", 
                            
                            performance.now()- startTime);
                        component.set('v.childRecords', actionResult.getReturnValue());

                var state = actionResult.getState();
            
            
        });
        var startTime = performance.now();
        $A.enqueueAction(action);
    }
})


This function does every thing you need to cache your data for caching. Lets take an example. 

I have a component in which I have display more than 1000 records in my component and that on press of button.

In my demo below, when action calls the server it takes around 500 ms and when clicked second time it takes around 2 ms. This is how you can cache the data without calling the server. This is the magic being handled by aura framework.



Watch the demo below:-



#Salesforce #Lightning #SetStorable #BestPractices #caching #cache


Thanks
Happy Coding :) :)



oAuth Use Case -  Headless API Explained in the Easiest way Wondering why? and When we use the Headless API flow Use case example: Imagine a...