Newer Version Available
Return Result for Asynchronous Code
Step 1: Define aura:method in Markup
Let’s look at an echo aura:method that uses a callback. We’ll use the c:auraMethodCallerWrapper.app and components outlined in Calling Component Methods. Here’s the echo aura:method in the c:auraMethod component.
The echo aura:method has an aura:attribute with a name of callback. This attribute enables you to set a callback that’s invoked by the aura:method after execution of the server-side action in SimpleServerSideController.
Step 2: Implement aura:method Logic in Controller
The echo aura:method invokes echo() in auraMethodController.js. Let’s look at the source.
echo() calls the serverEcho() server-side controller action, which we’ll create next.
Step 3: Create Apex Server-Side Controller
The echo aura:method calls a server-side controller action called serverEcho. Here’s the source for the server-side controller.
The serverEcho() method returns a String.
Step 4: Call aura:method from Parent Controller
Here’s the controller for c:auraMethodCaller. It calls the echo aura:method in its child component, c:auraMethod.
callAuraMethodServerTrip() finds the child component, c:auraMethod, and calls its echo aura:method. echo() passes a callback function into the aura:method.
The callback configured in auraMethodCallerController.js logs the result.
Step 5: Add Button to Initiate Call to aura:method
The c:auraMethodCaller markup contains a lightning:button that invokes callAuraMethodServerTrip() in auraMethodCallerController.js. We use this button to initiate the call to the aura:method in the child component.
Here’s the markup for c:auraMethodCaller.