How are JavaScript helpers and controllers used in Lightning?

How are JavaScript helpers and controllers used in Lightning?

JavaScript Controllers and Helpers play a key role in Lightning Components Framework.. In this blog we will discuss the difference between JavaScript Controllers and JavaScript Helpers in Salesforce Lightning Components framework.

How are helper functions used in a JavaScript bundle?

Helper functions are local to a component, improve code reuse, and move the heavy lifting of JavaScript logic away from the client-side controller, where possible. A helper function can be called from any JavaScript code in a component’s bundle, such as from a client-side controller or renderer.

When to use a Helper instead of a controller?

Note: If there are multiple types of components (say, AccountItem.cmp and AccountDetails.cmp), each component type will get a helper of their own. Use Controllers to listen to user events and other events like component, application events. But delegate business logic to helper.

How to create a helper for a component?

To create a helper using the Developer Console, click HELPER in the sidebar of the component. This helper file is valid for the scope of the component to which it’s auto-wired. Add a helper argument to a controller function to enable the function to use the helper. Specify (component, event, helper) in the controller.

How are variable types operate in the Lightning component framework?

Choosing the right type for the job is a core decision when building an API, so let’s take a look under the hood. In this article, we are going to look at how components operate with four different kinds of variable types and why it is important to make them a distinct type.

Do you know how to use Lightning components?

This article assumes you have a working knowledge of using Lightning Components. If you are new to Lightning Components, running through the Lightning Basics Module on Trailhead would be a good start.

Can you call a helper from a component?

You can only call methods from components in your own namespace, or global methods from another namespace. You need an instance of the component to call the helper. You cannot call it without first loading the component. This has implications on performance, so it should be used sparingly, and only as necessary.