0

I load some values using <aura:handler name="init" value="{!this}" action="{!c.loadOptions}" /> in my component. I want these values to be populated when my component is loaded. Basically it is records I show as a dropdown list. I call the server side controller function to get the records and populate them in an attribute.

My question: I used a callback function when setting up the values of the response received from the server side function. But is there a way to get the results right away meaning without callback. I am thinking if I use callback there could be a delay in showing the result to User. In general I am trying to see if this can be possible without implementing a callback function. Below is my high level component, client side controller and server side controller:

component:

<aura:component controller="AController">
 <aura:handler name="init" value="{!this}" action="{!c.loadOptions}" />
 <aura:attribute name="theValues" type="Account" description="" />
<lightning:layout>
        <lightning:select name="mySelect" label="Accounts" aura:id="mySelect" value="{!v.selectedValue}">
            <option value=""> </option>
            <aura:iteration items="{!v.options}" var="item">
                <option text="{!item.Name}" value="{!item.value}" selected="{!item.selected}"/>
             </aura:iteration>
        </lightning:select>

<lightning:layout>
</aura:component>

Client side controller:

({
   loadOptions: function (component, event, helper) {
        // Create the action
        var action = component.get("c.getAccounts");

        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.options", response.getReturnValue());
            }
            else {
                console.log("Failed with state: " + state);
            }
        });         
      // Send action off to be executed
      $A.enqueueAction(action);       
    }

})

Server side controller:

public with sharing class AController {
        @AuraEnabled
        public static List<Account> getAccounts() {
               // Check to make sure all fields are accessible to this user
            return [SELECT Id, Name FROM Account];
        }

    }

Generally asking, Can server side controller be called without using callback style and $A.enqueueAction(action);. I am wondering if this could delay the response from the server and user might not be able to see the result on time.

2
  • 1
    No, you can't avoid the callback (and the delay is very small if you limit your result set). Also, try caching your results for subsequent instant reloads. Commented Dec 9, 2017 at 3:15
  • Yes I would expect delay to be small in this case. I am assuming then all the server side calls have to be though callback. correct?
    – SfdcBat
    Commented Dec 9, 2017 at 3:18

1 Answer 1

2

The only way you could not use callbacks is if you were to hardcode the values into the component, but since you are getting a list of accounts, this is not possible.

Every component on Lightning is thought to have great performance for the user, doesn't matter if the server response takes too long to show up. When you access a page like Process Builder, for example, you are getting a page that is filled with callbacks. Basically every action you make in there is firing a callback that calls the server, get info, and then return to the page. While the data is being fetched the component is set to show the loading spinning wheel in your screen, so you could try to imitate that.

Also, as far as I know, there is no synchronous requests made to the server with the Lightning Framework, only asynchronous.

Considering that it is pretty rare for Salesforce's servers to perform poorly, if you are to receive delayed responses, it will be more likely because of your users' internet connection, and that would happen anyway if you were able to use synchronous requests or Visualforce.

1
  • Good to know this. I see that every call to the server side can be done only using $A.enqueueAction(action); which adds the action to the Lightning Components framework request queue. It queues up the server request and as far as the controller action is concerned, that’s the end of it. It is not guaranteed when, or if, we'll hear back. I guess then it makes sense to use callback to handle the response when received.
    – SfdcBat
    Commented Dec 9, 2017 at 16:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .