Wednesday, 14 December 2016

Get Started with Lightning Components

 

Lightning Components Basics
~ 10 mins
~ 15 mins
~ 25 mins
~ 25 mins
~ 45 mins
~ 35 mins
~ 35 mins
~ 45 mins
~ 5 mins

What Is the Lightning Components Framework?

Lightning Components is a UI framework for developing web apps for mobile and desktop devices. It’s a modern framework for building single-page applications with dynamic, responsive user interfaces for Force.com apps. It uses JavaScript on the client side and Apex on the server side.

 

An Example Lightning Component

Let’s take a look at a real Lightning component, and see what all that talk is about. First, here’s what the component looks like when rendered on screen:


It might not look like much, but there’s a fair bit going on. Here’s the code for it; this is from a component we’ll dig into in detail later.
<aura:component>
    <aura:attribute name="expense" type="Expense__c"/>
    <aura:registerEvent name="updateExpense" type="c:expensesItemUpdate"/>

    <div class="slds-card">
        <!-- Color the item blue if the expense is reimbursed -->
        <div class="{!v.expense.Reimbursed__c == true ?
            'slds-theme--success' : 'slds-theme--warning'}">

            <header class="slds-card__header slds-grid grid--flex-spread">
                <a aura:id="expense" href="{!'/' + v.expense.Id}">
                    <h3>{!v.expense.Name}</h3>
                </a>
            </header>

            <section class="slds-card__body">
                <div class="slds-tile slds-hint-parent">
                    <p class="slds-tile__title slds-truncate">Amount:
                        <ui:outputCurrency value="{!v.expense.Amount__c}"/>
                    </p>
                    <p class="slds-truncate">Client:
                        <ui:outputText value="{!v.expense.Client__c}"/>
                    </p>
                    <p class="slds-truncate">Date:
                        <ui:outputDate value="{!v.expense.Date__c}"/>
                    </p>
                    <p class="slds-truncate">Reimbursed?
                        <ui:inputCheckbox value="{!v.expense.Reimbursed__c}"
                            click="{!c.clickReimbursed}"/>
                    </p>
                </div>
            </section>
        </div>
    </div>

</aura:component>


Even before you know anything about Lightning Components, you can still notice a few things about this sample. First of all, it’s XML markup, and mixes both static HTML tags with custom Lightning Components tags, such as the <aura:component> tag that leads off the sample. If you’ve worked with Visualforce, the format of that tag is familiar: namespace:tagName. As you’ll see later, built-in components can come from a variety of different namespaces, such as aura: (as here), or force: or ui:


   Speaking of ui:, you might have noticed that there are input and output components, like <ui:inputCheckbox> and <ui:outputText>. Again, this is a pattern familiar to Visualforce developers. If you’re not one of those, hopefully it’s pretty obvious that you use the input components to collect user input, and the output components to display read-only values.

   For now, one last thing to notice is the use of static HTML with a number of CSS class names that start with “slds”. We’re going to use the Salesforce Lightning Design System, or SLDS, to style our components, and while we won’t explain SLDS in detail in this module, we want you to see examples of it in action.
OK, cool, Lightning Components markup is XML. But didn’t we say something about JavaScript earlier? Notice the click="{!c.clickReimbursed}" attribute on the checkbox? That means “when this checkbox is clicked, call the controller’s clickReimbursed function.” Let’s look at the code it’s attached to. 


({
    clickReimbursed: function(component, event, helper) {
        var expense = component.get("v.expense");
        var updateEvent = component.getEvent("updateExpense");
        updateEvent.setParams({ "expense": expense });
        updateEvent.fire();
    }
})




This is the component’s client-side controller, written in JavaScript. The clickReimbursed function in the component’s controller corresponds to the click="{!c.clickReimbursed}" attribute on the checkbox in the component’s markup.
In Lightning Components, a component is a bundle of code. It can include markup like the earlier sample, in the “.cmp resource,” and it can also include JavaScript code, in a number of associated resources. Related resources are “auto-wired” to each other, and together they make up the component bundle.

What About AngularJS, React, and Those Other JavaScript Frameworks?

Another question that comes up frequently is: “How does Lightning Components compare to MyFavoriteFramework?” where that favorite framework is another modern JavaScript web app framework such as AngularJS, React, or Ember.
These are all fine frameworks! Many people know them, and there are a lot of resources for learning them. You might be surprised to learn that we think these frameworks are a great way to build Force.com apps!
We recommend using them with Visualforce, using what we call a container page, and packaging your chosen framework and app code into static resources. Using an empty container page has Visualforce get out of your way, and lets you use the full capabilities of your chosen framework.
While it’s possible to use third-party JavaScript frameworks with Lightning Components, it’s a bit cumbersome. Lightning Components doesn’t have the notion of an empty page, and has some specific opinions about how, for example, data access is performed, and some rather specific security requirements.
And frankly, the features of Lightning Components and most modern frameworks overlap quite a bit. While the style or specifics might be different, the features provided are conceptually similar enough that you’re effectively running duplicate code. That’s neither efficient nor easy to work with.
Another thing to consider: general-purpose frameworks such as AngularJS are designed to be agnostic about the platform they run on top of, in particular data services. Lightning Components, on the other hand, is designed to connect natively with services provided by Salesforce and the Force.com platform. Which do you think is going to help you build apps faster?

Where You Can Use Lightning Components

You can use Lightning Components to customize your Salesforce org in a number of different ways. But that’s not all! You can use Lightning Components to create stand-alone apps that are hosted on Salesforce. And you can even create apps that are hosted on other platforms, including embedding them into apps from those platforms.

Add Apps to the Lightning Experience App Launcher

Your Lightning Components apps and custom tabs are available from the App Launcher, which you reach by clicking App Launcher iconin the header.



Click a custom app  to activate it. Items in the app display in the navigation bar, including any Lightning components tabs you’ve added to the app. Note that you need to add your components to tabs for them to be accessible in the App Launcher. Lightning components tabs that aren’t in apps can be found in All Items.

Add Apps to Lightning Experience and Salesforce1 Navigation

As described in the preceding example, you can add Lightning components tabs to an app and they display as items in the app’s navigation bar.



Create Drag-and-Drop Components for Lightning App Builder and Community Builder



Build custom user interfaces using your own Lightning components, or those you install from AppExchange, for desktop and mobile devices.

Add Lightning Components to Lightning Pages

A Lightning Page is a custom layout that lets you design pages for use in the Salesforce1 mobile app or in Lightning Experience. You can use a Lightning Page to create an app home page and add your favorite Lightning component, such as the Expenses app we’ll be creating in this module, to it.

Add Lightning Components to Lightning Experience Record Pages

Just as the title suggests, you can customize Lightning Experience record pages by adding a Lightning Component.



Launch a Lightning Component as a Quick Action

Create actions using a Lightning component, and then add the action to an object’s page layout to make it instantly accessible from a record page.



Create Stand-Alone Apps

A standalone app comprises components that use your Salesforce data and can be used independently from the standard Salesforce environment.



Run Lightning Components Apps Inside Visualforce Pages

Add Lightning components to your Visualforce pages to combine features you’ve built using both solutions. Implement new functionality using Lightning components and then use it with existing Visualforce pages.

Run Lightning Components Apps on Other Platforms with Lightning Out

Lightning Out is a feature that extends Lightning Apps. It acts as a bridge to surface Lightning Components in any remote web container. This means you can use your Lightning Components inside of an external site (for example, Sharepoint or SAP), in a hybrid app built with the Mobile SDK, or even elsewhere in the App Cloud like on Heroku.




No comments:

Post a Comment