Friday, March 16, 2018

Getting started with basics of Lightning Component


In below image, I have tried to explain at high level, how different JavaScript files are related in Lightning components, there are many other parts as well, but I tried to keep it short and simple.
Getting started with lightning components
Getting started with lightning components
Component Bundles
Lightning components consists of many resources as explained in below table.
ResourceResource NameUsage
Component or Applicationsample.cmp or sample.appThe only required resource in a bundle. Contains markup for the component or app. Each bundle contains only one component or app resource.
CSS Stylessample.cssStyles for the component.
ControllersampleController.jsClient-side controller methods to handle events in the component.
Designsample.designRequired for components used in the Lightning App Builder or Lightning Pages.
HelpersampleHelper.jsJavaScript functions that can be called from any JavaScript code in a component’s bundle.
Documentationsample.auradocA description, sample code, and one or multiple references to example components.
RenderersampleRenderer.jsClient-side renderer to override default rendering for a component.
SVGsample.svgCustom icon resource for components used in the Lightning App Builder.
Lightning Application
Lightning applications are standalone page, which contains many other lightning components. Its mark up is similar to HTML as shown in above image. To create Lightning application, open developer console, navigate to “File | New | Lightning Application”. It will prompt for “Name” and “Description”, provide proper information.
This post is written in order of “execution flow” as depicted in above image. However, to save code in your Salesforce instance, you need to create it in opposite direction. So, basically you need to start with step 5.
AllContacts.app
1<aura:application >
2    <link href='/resource/bootstrap/' rel="stylesheet"/>
3    <c:AllContactsComp />
4</aura:application>
In above lightning application, I have used Bootstrap to style my application, saved in static resource by name “bootstrap”. This application has only one Lightning component named “AllContactsComp”.
Lightning Component
Lightning components can be created by navigating to “File | New | Lightning Component” in developer console. It can contain “HTML + Aura components” supported by Salesforce.
Lightning Components
Lightning Components – Image 2
As shown in above image, Lightning component consists of Controller ( JS ), Helper ( JS ), Style ( CSS ), Documentation, Rerenderer, design and SVG. We will be using only Controller and Helper in this post.
AllContactsComp.cmp
1<aura:component controller="ContactAura">
2    <aura:attribute name="contactRows" type="Object[]"/>
3    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
4    <div class="container">
5        <p>
6            <b> List of Contacts </b>
7        </p>
8        <table class="table table-bordered table-hover">
9            <thead>
10                <tr>
11                    <th><strong> Id </strong></th>
12                    <th> <strong>  Name </strong> </th>
13                </tr>
14            </thead>
15            <tbody>
16                <aura:iteration var="cell" items="{!v.contactRows}">
17                    <tr>
18                        <td> <a href="{! '/'+cell.Id}"> {!cell.Id} </a> </td>
19                        <td> {!cell.Name}  </td>
20                    </tr>
21                </aura:iteration>
22            </tbody>
23        </table>
24    </div>
25</aura:component>
  • Above component is using Apex class “ContactAura”, which I will explain in later part of this post.
  • Attribute of type Object[] defined as name “contactRows”.
  • Its calling method doinit() in handler file. “c” stands for “Controller” and “v” stands for “View” in Aura syntax.
  • Later part of code is iterating over variable “contactRows” and displaying ID and name.
JavaScript Controller
We can click on “Controller” link available on right of component (image 2). It will create new file by name “[component]Controller.js” , in this case file “AllContactsCompController.js” will be created.
AllContactsCompController.js
1({
2    doInit : function(component, event, helper){
3        helper.getAllContacts(component);
4    }
5})
this method “doInit()” is called by Component. This method is calling method in helper file.
JavaScript Helper
click on “Helper” link available on right of component (image 2). It will also create new JavaScript file which will be used by Component. This file mostly interacts with Apex method defined in @AuraEnabled static public method.
AllContactsCompHelper.js
1({
2    getAllContacts : function(component, event, helper) {
3        var action = component.get("c.get10COntacts");
4        action.setCallback(thisfunction(a){
5            component.set("v.contactRows", a.getReturnValue());
6        });
7        $A.enqueueAction(action);
8    }
9})
This helper file is calling method “get10Contacts” defined in Apex controller. After successful callback, it is setting variable “contactRows” defined in “View” of component. It is very important to enqueueAction so that it can be processed.
Apex Controller
ContactAura
1public class ContactAura {
2    @AuraEnabled
3    public static List<Contact> get10COntacts()
4    {
5        return [SELECT Id, NAME FROM Contact Limit 10] ;
6    }
7}
This apex controller is just querying 10 contacts to show on component.
Output
You can check final output by clicking on “Preview” on right of Lightning App file explained in step 1 of image.
Getting started with Lightning component Output
Getting started with Lightning component Output

No comments:

Post a Comment