Guest post: Peter Knolle is a Solutions Architect at Trifecta Technologies, Force.com MVP, and Salesforce Developer Group Organizer. He holds six Salesforce certifications, blogs regularly at peterknolle.com, and is active on Twitter @PeterKnolle.
Browsing the Lightning Components Documentation
The framework provides three parameters to component controller functions: component, event, and helper. For example, a controller function to handle a form submission could be the following.
It’s worth making it clear what is happening and how powerful it is. All that you have to do is define your controller function to accept the three parameters and you don’t have to worry about where they come from at all. You don’t have to configure anything else or program something to find the objects you need. The parameters are simply available to you to use in your controller function. The framework takes care of populating them with the correct values.
The helper is a place where you can define code to be shared within your component or sub components. In general, if you have any non-trivial logic you should move it to a helper and let the controller call the helper to execute it. For example, you might have some code that calls a server-side controller action to get a list of Opportunity records based on an event parameter. Instead of coding all of server communication directly in the controller, code it in the helper and leave the controller as very simple.
Now, if you have code elsewhere in your component that needs to load opportunities you can easily reuse it. For example, you might have code that loads based on an attribute value.
A Lightning Component is represented as a component object in the API. The methods on the component that you’ll use most often are as follows.
- get() / set() – Get and set values from a value provider. The “v” value provider is the attribute set value provider (properties) and the “c” is the controller (methods). Examples:
- find() – Finds a component, within this current component, with a specified aura:id. One thing worth noting is that the component that it finds could be a simple HTML element. The framework treats all HTML elements a first-class components. Examples:
- getEvent() – Gets a component-level event. This is used when an component wants to fire an event that it has registered. Example:
A Lightning Event is represented as an Event object in the API. Just like the component, the event is provided by the framework to controller functions as a parameter. The methods on the event that you’ll find yourself using are as follows:
- getParam() – Gets an attribute that was set on the event. Example:
- fire() – Fires the event.
- setParams() – Sets parameters on the event. Example:
In addition to being able to call the methods individually, method chaining is supported for most objects in the API. For example, the following will get the event, set the parameters, and fire it.
The Action object isn’t a controller parameter, but is one that you will use when communicating with server-side controllers.
- getReturnValue() – Gets the result of calling the action. Example:
- getState() – Gets the state of the action. You’ll use this most often to check if the result of calling a server-side action was a success or error. Example:
- getError() – Gets an array of errors generated from a server-side action. Hopefully this never has anything in it for you, but if there is a server side error you can access it through this. Note that you might still have to go to the server (Apex debug log) to determine the actual error as this error could just contain something like “internal server error”. Example:
- setParams() – Specify the arguments to the action. If your server-side method has parameters, set them by name with this. Example:
- setCallback() – Sets the callback to be called when the action completes. Example:
In addition to the component and event objects, the API has some useful utility functions. Most can be accessed through the Aura object, which can be referred to as $A for convenience.
- $A.enqueueAction() – Enqueues an action to be called by the framework. You’ll use this after you’ve configured your server-side action’s parameters and callback and you want to fire it. Instead of being able to fire it directly, you enqueue it and the framework executes it. Example:
- $A.log() – Logs information to any subscribers of the log level. By default nothing will happen if you call this function. You have to set up an accompanying subscriber to the level at which you are logging and have it handle it. Example:
- $A.getEvt() – You’ll use this method to get an application-level event to fire. Example:
- $A.util.isUndefined(), isUndefinedOrNull(), isNumber(), etc. – There are multiple methods that allow you to test something about a variable. Example:
For more information on the JavasScript API in the Lightning Component Framework, refer to the Lightning Component Developer’s Guide. For a quick overview and introduction to Lightning Components, visit the Lightning Components page on Salesforce Developers. If you’re ready to learn hands-on while earning badges to demonstrate your skills, start the Lightning Components module in Trailhead.