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.