Lightning Components

Lightning Components are a new UI framework for developing rich applications that are seamlessly mobile. Lightning Components, as their name suggests, are components, or building blocks for developing applications. Lightning is built on top of the open source Aura JavaScript application development framework. You can read more about the Aura open source project at https://github.com/forcedotcom/aura. Aura is event driven, and as a result, so are lightning components. Whenever you are working with components, you'll often run into tags that start with Aura. For instance, when you create a new component, the system will scaffold out the bare minimum component needed to work, something like this:

<aura:component>
  <!-- your component details here -->
</aura:component>

Aura components are actually bundles of many different files separated nicely by concern. Not all of the files need to be populated; for instance, the renderer file isn't required. In the following screenshot, you can see the COMPONENT file open with the initial scaffold data in place. Note that while we are using the developer console in our screenshots, components are one area of Salesforce development where the developer console is the best of breed tool. There are plugins for Sublime and Eclipse, but by far the best experience is found with the developer console.

Aura and, as a result, Lightning Components utilize attributes and expressions for holding and manipulating data. In this component fragment, for example, we establish a string attribute:

<Aura:Component>
  <Aura:Attribute Name="ClientName" Type="String" Default="Marc Benioff"/>
</Aura:Component>

This attribute and its value can be accessed using the v object in expressions. These expression objects have single character names and can be a bit confusing. However, essentially they boil down to this:

  • V: This stands for the value of attributes
  • C: This stands for the controller, though this can refer to the client-side JavaScript controller or the Apex controller attached to the component, or to the component itself
  • UI: This stands for the user interface elements

These objects can be accessed within the scope of expressions. These are similar to Visualforce expressions and have a similar syntax. For instance, to use the value of an attribute as the text of a button, you would access it this way:

<Aura:Component>
  <Aura:Attribute Name="ClientName" Type="String" Default="Marc Benioff"/>
<ui:button label="call {!v.ClientName}" press="{!c.MakeCall}"/>
</Aura:Component>

Here, we added a single line of code that has two expressions in it—one a reference to the value of the ClientName attribute and another to call the MakeCall method on the controller. Here is a screenshot as an example:

Lightning Components

Clicking on the CONTROLLER option on the right or using the Ctrl + Shift + 2 key combo will open up the javascript controller file, as shown in the following screenshot. It's not entirely intuitive what these files do by name, so here's a breakdown:

Lightning Components

Component files

The COMPONENT file is used for the markup of the component setting up the core UI of your component. If your component needs a pick list, for instance, the component file is where you place the <select> tags.

The CONTROLLER file is where your JavaScript controller lives. This controller encapsulates the actions and logic your component uses. The standard interface for a Lightning Component controller is as shown here:

({
  customAction : function(comp, evt, helper) {
    //Your custom logic here
  }
})

As intercomponent communication occurs by firing events, your controller methods are often event handlers or event emitters. Emitter methods take the following general form and utilize the .fire() method to fire off the event:

customEmitter : function(cmp, event, helper) { 
var myObj = cmp.get("v.myObj"); 
var myEvent = $A.get("e.namespace:someEvent");
myEvent.setParams({ "myObj": myObj}).fire();
}

Note the use of the $A global variable. This is your access to the Aura object. It provides a number of utility and access methods including the .get() method, which functions similarly to the jQuery selector system. Using get you can access application wide events.

The HELPER file exists to contain helper methods that can be reused in multiple places throughout the component. Specifically, it helps you write logic that is passed by reference to both controllers and renderers. This allows you to keep your renderers and controllers relatively clean, easy to read, and minimizes duplicate logic. For instance, if you have code that calls back to an Apex controller and you need to access it from both your controller and renderer, you can place that code inside the helper and reference it from both using helper.commonMethod:

// Your controller 
callApexController: function(cmp, helper) {
  helper.hCallApexController ();
}

// Your renderer 
afterRender: function(cmp, helper) {
   this.superAfterRender();
   helper.hCallApexController ();
} 

// Your helper
hCallApexController: function(cmp, helper) {
var a = component.get("c.getS1Data"); 
a.setCallback(this, function(action) { 
if (a.getState() === "SUCCESS") {
	//handle the data returned 
} else {
//handle the error
} 
});

   $A.enqueueAction(a);
}

In the preceding example, we extracted the logic for calling an Apex controller method and handling the results to the helper file. We can use that logic easily by calling helper.hCallApexController(). Aside from promoting reusability, this helps us ensure that when we fix bugs related to our Apex callout, there is only one place to fix them! Also, take note of the naming convention I used for the helper method. You don't have to preface your helper methods with h but adopting a logical and consistent naming convention allows your teammates and your future self to easily know that this method is not defined later in the controller, but in the helper file.

In our preceding example code, you saw references to both a controller and a renderer. We have not discussed renderers yet because you don't need to worry about creating a customer renderer generally. In fact, you only need to create or edit the renderer if you need to modify the way the standard renderer works. When overriding or customizing the renderer, ensure that you always call this.superRender(); first. Once you've done that, you can include custom logic or even call helper methods like the example does.

Renderers provide a place for you to override the standard render methods. This allows you to, for instance, trigger logic when a particular render method runs. For instance, look at this simple re-render override:

rerender : function(cmp, helper){
    this.superRerender();
    helper.disableSubmitButton(cmp);
}

In this case, the component that will fire rerender automatically whenever a component attribute is modified will fire this custom version, rather than the default rerender method. Our custom rerender method does two things. First, it calls the main rerender method, as triggered by this.superRerender(); and then calls our helper.disableSubmitButton(); method. This effectively allows us to call custom logic (in this case, our disableSubmitButton logic), whenever one of the standard render methods is called (in this case, the rerender method). Disabling a submit button or other UI-based properties is perfect for this sort of automatically chained logic.

So when do you need your own render object? You'll need a custom renderer if, for example, you want to update a component whenever a second component is changed. In that case, your second component's renderer would call a helper method to update the first component.

The STLYE file contains all the CSS needed to style your component as you see fit. This is, perhaps, the most straightforward of the component's files. There is really only one thing that differentiates this CSS file from the numerous others you've worked on in the past. The scaffolding for the style file is as follows:

.THIS {
}

The .THIS tag is the essential difference. When your component is utilized, the framework dynamically replaces the .THIS element with the component's instance name. This allows you to override styles for your component without stepping on another component's identically named .awesomeDiv style. In other words, if you have two instances of a custom button component, the framework will automatically replace the .THIS element with the instance name of each component. That way, the styling of one instance won't interfere or overwrite the styling of the second instance. For example, if you programmatically change one component's .THIS styling, the second instance of that component is not affected!

The DOCUMENTATION file is where all of the utilization documentation for your component goes. While this is, I believe, primarily for components based on AppExchange to write packaged documentation for their components, as application developers, we should never forget to take every opportunity to document design decisions, tradeoffs, and assumptions. Eventually, even the most well-written software exhibits bugs, and the better written the code is, the harder it can be to decipher after months or years of writing it. Use the documentation file to help your future self and your team! Also note, that you can access the full documentation of your org's Lightning components by visiting the auradocs application in your org—AuraDocs at https://<Instance> lightning.force.com/auradocs. Keep in mind, however, that if you've not written documentation for your custom components, it won't be visible in the auradocs app.

The DESIGN file can be a bit hard to understand. In a way, it's a header file for your component. Like C header files, the design file describes the methods, attributes, and events your component exposes so that the Lightning App Builder and other visual tools can properly guide the user in using your component. For instance, you might expose an attribute in a design file like this:

<design:component>
    <design:attribute name="Customer" label="Customer" description="Customer contact Id you wish to interact with" />
</design:component>

In this design file, we enabled a single attribute named Customer. This will be displayed in the Lightning App Builder with the Customer label and the description will be Customer Contact ID you wish to interact with. These label and description parameters help end users who are creating applications with your components to see what data to pass in.

Lastly, but certainly important is the svg file. This file contains the SVG data of your components icon. Even if you are not developing your component for distribution, you should provide an icon to help distinguish it from other components in the Lightning App Builder.

@auraEnabled Apex

As mentioned earlier, you can call the Apex controller methods from within your Lightning Component. This is one of the features that Lightning adds to the Aura project. To make such calls, however, you have to annotate your controller methods with the @auraEnabled tag. Here's an example:

public class LAccountController {
    @AuraEnabled
    public static List<Account> getAccountsWithBillingAddress() {
        return [SELECT Name, Id, BillingAddress FROM Account Order 
By Id Limit 20];
    }
}

Here, our Apex controller exposes only a single method, but since it is @auraEnabled, we can call it from our Lightning Component. However, we'll need to associate the controller and the component together by modifying our component markup to include the controller, as follows:

<aura:component controller="Namesapce.LAccountController">
 <!-- component details -->
</aura:component>

Note the additional controller attribute on the first line. This associates the component with the LAccountController controller found in the Namespace namespace. One of the things you have to do to start with Lighting Component development is define a namespace for your org. If you select CompanyName as your namespace, your invocation of the controller would read CompanyName.LAccountController. When your component is initialized, it will have access to the @auraEnabled methods in your Apex controller. It should also be noted that any Apex class will have to be tested to the same standards as non AuraEnabled classes are.

Lightning future

Lightning is, I believe, the future of UI and custom app development on the Salesforce platform. It's write-once, run on all screen sizes feature set means that any app is mobile by default. That said, I don't think Visualforce is going away any time soon. Lightning is the future, but not the exclusive future. One reason I believe Lightning is the future stems from Salesforce's tireless march to make development of business applications easier, and, well, less developer focused. Lighting Process Builder, for instance, enables admins to create complex logic without writing a single line of code. Developers who write code are still needed to develop actions and write components, but the shift isn't about eliminating developers. Indeed, the shift is in enabling more people to develop more apps faster by relying on off-the-shelf actions and components. Nowhere is this shift more easily seen than in the final feature of Lightning—the Lightning App Builder.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.7.240