Thursday, 15 December 2016

Creating and Editing Lightning Components

Creating and Editing Lightning Components

 In your DE org, open the Developer Console under Your Name or the quick access menu 

You’re ready to write Lightning Components code!

Create Lightning Components in the Developer Console

So, let’s write something. Select File | New | Lightning Component to create a new Lightning component. In the New Lightning Bundle panel, enter helloWorld for the component name, and click Submit.





This creates a new helloWorld component bundle, with two open tabs. Close the helloWorld tab, and keep the helloWorld.cmp tab open. helloWorld.cmp contains the opening and closing tags for a Lightning component, <aura:component>. Between them, add the following markup, and save:

<p>Hello Lightning!</p>



 You can’t run your component directly and see how it behaves. Instead, your component needs to run inside a container app, which we’ll call a container for short. Examples of containers would be the Lightning Experience or Salesforce1 apps, or an app you build with Lightning App Builder—basically, any of the things you saw at the end of the prior unit. You add your component to one of these containers, and then access it within that container.

Select File | New | Lightning Application to create a new Lightning app. In the New Lightning Bundle panel, enter harnessApp for the app name, and click SubmitThis creates a new harnessApp bundle, with two open tabs. Close the harnessApp tab, and keep the harnessApp.app tab open. harnessApp.app contains the opening and closing tags for a Lightning app, <aura:application>. Between them, add the following markup, and save:


<c:helloWorld/>

This adds the helloWorld component we created earlier to the harnessApp app. Before we explain this deceptively simple app, click back and forth between the harnessApp.app and helloWorld.cmp tabs in the Developer Console. Besides the markup, what do you notice that’s different?


Got it in one: the Preview button. Apps have one, components don’t. Click it now, and another browser window should open and show you your app.




 The URL for our “preview” is actually the permanent home of our app (once it’s made available to our users). The format of the URL is the following:

https://<yourDomain>.lightning.force.com/<yourNamespace>/<yourAppName>.app.
<yourAppName> represents the name of your app bundle, in this case, harnessApp.  


What Is a Component?

As a practical matter, a component is a bundle that includes a definition resource, written in markup, and may include additional, optional resources like a controller, stylesheet, and so on. A resource is sort of like a file, but stored in Salesforce rather than on a file system.
Our helloWorld.cmp component definition resource is easy to understand.

<aura:component>
 <p>Hello Lightning!</p> 

</aura:component>

A bundle is sort of like a folder. It groups the related resources for a single component. Resources in a bundle are auto-wired together via a naming scheme for each resource type. Auto-wiring just means that a component definition can reference its controller, helper, etc., and those resources can reference the component definition. They are hooked up to each other (mostly) automatically.
Let’s see how this works. With helloWorld.cmp active, click the STYLE button in the component palette on the right. This opens a new tab for the style resource that was added to the helloWorld bundle. It starts with a single, empty selector, .THIS. To see how this works, add a simple style to the stylesheet, so that it looks like the following.

1
.THIS {
2
}

3

4
p.THIS {

5
    font-size: 24px;
6
}

Then reload your preview window for harnessApp.app. VoilĂ , larger text! But, how does .THIS work? It’s the magic of auto-wiring! At runtime .THIS is replaced with a style scoping string named for your component. It limits style rules to only this component, so that you can create styles that are specific to the component, without worrying about how those styles might affect other components.
So now our helloWorld bundle has two resources, the component definition, helloWorld.cmp, and the stylesheet, helloWorld.css. You can think of it like a folder, or an outline:
  • helloWorld — the component bundle
    • helloWorld.cmp — the component’s definition
    • helloWorld.css — the component’s styles
As you can see in the Developer Console, there are a number of other resource types you can add to a component bundle. Go ahead and click the CONTROLLER and HELPER items to add those resources to the bundle. Now your bundle looks something like this, and you can start to see the naming system.
  • helloWorld — the component bundle
    • helloWorld.cmp — the component’s definition
    • helloWorldController.js — the component’s controller, or main JavaScript file
    • helloWorldHelper.js — the component’s helper, or secondary JavaScript file
    • helloWorld.css — the component’s styles
In this module, we’ll work with only these four resource types. We’ll talk a lot more about the controller and helper resources when we actually start writing code for them. For now, you can just leave the default implementations. After all, this is just hello world!


What Is an App?

Now that we know what a component is, it’s actually easy to explain what an app is—an app is just a special kind of component! For the purposes of this module, you can think of an app as being different from a component in only two meaningful ways:
  • An app uses <aura:application> tags instead of <aura:component> tags.
  • Only an app has a Preview button in the Developer Console.

What Are Apps For?

As simple as that sounds, there are a few practical details in how you can use an app vs. a component. The main items are the following.
  • When writing markup, you can add a component to an app, but you can’t add an app to another app, or an app to a component.
  • An app has a standalone URL that you can access while testing, and which you can publish to your users. We often refer to these standalone apps as “my.app.”
  • You can’t add apps to Lightning Experience or Salesforce1—you can only add components. After the last unit this might sound weird; what exactly do you add to the App Launcher, if not an app? What you add to App Launcher is a Salesforce app, which wraps up a Lightning component, something defined in a <aura:component>. A Lightning Components app—that is, something defined in a <aura:application> —can’t be used to create Salesforce apps. A bit weird, but there it is.
You publish functionality built with Lightning Components in containers. Lightning Components apps are one kind of container for our Lightning components. You build all of your “app” functionality inside a top-level component. Then at the end, you stick that one component in a container—maybe a Lightning Components app, maybe Salesforce1, maybe something else. If you use a my.app, the container can set up services for your main component, but otherwise it’s just there to host the component.
Let’s take another look at the app we created. Here again is the harnessApp.app definition resource:


<aura:application>
2
       

3
   <c:helloWorld/>
4
        


</aura:application>
No matter how much functionality we decide we’re going to add to our helloWorld “app”, it’s all going to go inside the helloWorld component. It could have a Google Docs-style editor embedded in it for revising the hello message, but our harnessApp.app definition is going to remain pretty much this simple.
From here on, we’ll assume that you’re using an actual Lighting Application bundle as just a container, or harness, for components you create. Feel free to keep using harnessApp.app! But, when we talk about creating apps, we really mean building functionality inside a component bundle, not an application bundle, because that’s how you build “apps” in the real world.


Components Containing Components, Containing…Components!

The harnessApp.app definition is also interesting because instead of static HTML we have our helloWorld component. We say that harnessApp contains the helloWorld component. Let’s dive into this a little bit, and make helloWorld a little more complex.
In the Developer Console, create a new Lightning component named helloHeading. For its markup, paste in the following code.

1
<aura:component>
2
    <h1>W E L C O M E</h1>

3
</aura:component>

Now, click back to helloWorld.cmp, and add <c:helloHeading/> to it, above the “Hello Lightning” line. Your helloWorld component definition should now look like this:

1
<aura:component>
2
       

3
    <c:helloHeading/>
4
         

5
    <p>Hello Lightning!</p>
6
         

7
</aura:component>
Reload the app to see the change. Your component structure, in terms of what contains what, now looks like this:
  • harnessApp.app
    • helloWorld.cmp
      • helloHeading.cmp
      • (static HTML)
We say that helloHeading is a child component of helloWorld, or that helloHeading is nested inside helloWorld, or…. There are any number of different ways to say that helloWorld contains helloHeading. What’s more, you can keep nesting components inside other components down to pretty much any level you’d care to. It starts being too hard to keep straight in your head well before you run into a limitation of Lightning Components!


This process of putting components inside each other is fundamental to building Lightning Components apps. You start with, or build, simple, “fine-grained” components, where each component provides a defined set of self-contained functionality. Then you assemble those components into new components with higher-level functionality. And then you use those components, and “level up” again. 

1 comment:


  1. Just wish to say your article is as astounding. The clearness in your
    submit is just excellent and i can suppose you’re knowledgeable in this subject.
    Well along with your permission allow me to take hold of your RSS feed to stay up
    to date with drawing close post. Thanks a million and
    please carry on the rewarding work.
    Also See:
    AngularJS Certification Training in Chennai

    ReplyDelete