Skip to main content

Lightning Component Basic and Advanced

Lightning Component also refer as Aura Component Or Aura Framework is base on View-Controller Architecture. Refer the Below Image which has been taken from Trailhead Module.
Aura architecture


Server-Side Controller (Apex):

Server calls are expensive, and can take a bit of time. Milliseconds when things are good, and long seconds when the network is congested. You don’t want apps to be locked up while waiting for server responses.
The solution to staying responsive while waiting is that server responses are handled asynchronously. What this means is, when you click the Create Expense button, your client-side controller fires off a server request and then keeps processing. It not only doesn’t wait for the server, it forgets it ever made the request!
Then, when the response comes back from the server, code that was packaged up with the request, called a callback function, runs and handles the response, including updating client-side data and the user interface.
If you’re an experienced JavaScript programmer, asynchronous execution and callback functions are probably your bread and butter. If you haven’t worked with them before, this is going to be new, and maybe pretty different. It’s also really cool.

There are only two specific things that make this method available to your Lightning Components code.

  • The @AuraEnabled annotation before the method declaration.
  • The static keyword. All @AuraEnabled controller methods must be static methods, and either public or global scope.
Note: “Aura” is the name of the framework at the core of Lightning Components. You’ve seen it used in the namespace for some of the core tags, such as <aura:component>. Now you know where it comes from.

Load Data From Server:
The next step is to wire up the expenses component to the server-side Apex controller.
<aura:component controller="ExpensesController">

However, pointing to the Apex controller doesn’t actually load any data, or call the remote method. Like the auto-wiring between the component and (client-side) controller, this pointing simply lets these two pieces “know about” each other. This “knowing” even takes the same form, another value provider. But the auto-wiring only goes so far. It remains our job to complete the circuit.

In this case, completing the circuit means the following.
  1. When the expenses component is loaded,
  2. Query Salesforce for existing expense records, and
  3. Add those records to the expenses component attribute.

Handler Tag in Component :
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>

Calling Server-Side Controller Methods

    // Load expenses from Salesforce
    doInitfunction(componenteventhelper) {
        // Create the action
        var action = component.get("c.getExpenses");
        // Add callback behavior for when response is received
        action.setCallback(thisfunction(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.expenses"response.getReturnValue());
            }
            else {
                console.log("Failed with state: " + state);
            }
        });
        // Send action off to be executed
        $A.enqueueAction(action);
    },

The outline of what this code does:
  1. Create a remote method call.
  2. Set up what should happen when the remote method call returns.
  3. Queue up the remote method call.
var action = component.get("c.getSomething");
This line of code creates our remote method call, or remote action which has been Static and AuraEnabled in Apex.

Looks Similar?
Yes! It was “v.something” that we were getting, with v being the value provider for the view. Here it’s “c”, and yes, c is another value provider. And we’ve seen a c value provider before, in expressions like press="{!c.clickCreate}" and action="{!c.doInit}".

$A.enqueueAction(action);

We saw $A briefly before, but didn’t discuss it. It’s a framework global variable that provides a number of important functions and services. $A.enqueueAction(action) adds the server call that we’ve just configured to the Aura component framework request queue. It, along with other pending server requests, will be sent to the server in the next request cycle.
That sounds kind of vague. The full details are interesting, and important for advanced use of Aura components. But for now, here’s what you need to know about $A.enqueueAction(action).
  1. It queues up the server request.
  2. As far as your controller action is concerned, that’s the end of it.
  3. You’re not guaranteed when, or if, you’ll hear back.
In this specific callback function, Salesforce do the following.
  • Get the state of the response.
  • If the state is SUCCESS, that is, our request completed as planned, then:
  • Set the component’s expenses attribute to the value of the response data.
Do the Following Trialhead for hands On : Lightning Component Basic and Advanced

Comments

Popular posts from this blog

Maintain Your Platform Developer I Certification for Winter ’25

  Make Invocable Actions Easier to Configure with New InvocableVariable Modifiers Simplify the configuration of invocable actions using new modifiers from Salesforce. Both the defaultValue and placeholderText modifiers will appear in Flow Builder for the Action elements that correspond to an invocable method. Here’s how to use them. defaultValue Modifier : Set a default value for an input parameter. When the action is used, the input parameter will have a predefined value unless changed by the user. placeholderText Modifier : Set custom placeholder text for an input parameter. Text can provide examples or additional guidance to help users understand what to enter in the input field. Accessing these modifiers in Flow Builder makes it easier to configure and use the actions within your flows. This change applies to Lightning Experience and Salesforce Classic in Performance, Unlimited, Developer, Enterprise, and Database.com editions.

Maintain Your Administrator Certification for Spring ’25

  Manage Included Permission Sets in Permission Set Groups via Summaries Update user access more efficiently by specifying which permission set groups include a permission set directly from the permission set’s summary. Previously, to manage included permission sets, you were required to navigate to each permission set group’s page. From Setup, in the Quick Find box, enter  Permission Sets , and select Permission Sets. Select a permission set, and then click  View Summary . In the Related Permission Set Groups tab, click  Add  or  Remove . This change applies to Lightning Experience and Salesforce Classic (not available in all orgs) in Contact Manager, Group, Essentials, Professional, Enterprise, Performance, Unlimited, Developer, and Database.com editions. Sort List Views by Multiple Columns To see your data in a more intuitive way and make your list views more actionable, you can now sort list views on object home pages by up to five columns. Select the c...

Salesforce Architect Certification Maintenance (Winter '25)

  Update Records Using ISO State and Country Codes Standardize and simplify updating address records with two new fields that store the ISO code for country or territory and state or province. Improve data quality and accuracy using this new support for ISO codes, and reduce the risk of errors and inconsistencies that can occur when using names. Previously, the Address component stored only the name associated with the user’s selection. With this new support for ISO codes, you can update the country or territory and state or province fields on records with ISO codes instead of names. This feature can be used for new or existing screen flows. Here are the steps to create a new flow. In Setup, on the State and Country/Territory Picklists page, ensure that Enable Picklists for Address Fields is enabled. In Flow Builder, create a screen flow. Add a Screen element to the flow. Include an Address component in the Screen element. Optionally, specify a default value in the Country Code and...

Translate