Sharing JavaScript Code in a Component Bundle
A helper function can be called from any JavaScript code in a component’s bundle, such as from a client-side controller or renderer.
Helper functions are similar to client-side controller functions in shape, surrounded by parentheses and curly braces to denote a JavaScript object in object-literal notation containing a map of name-value pairs. A helper function can pass in any arguments required by the function, such as the component it belongs to, a callback, or any other objects.
1({
2 helperMethod1 : function() {
3 // logic here
4 },
5
6 helperMethod2 : function(component) {
7 // logic here
8 this.helperMethod3(var1, var2);
9 },
10
11 helperMethod3 : function(var1, var2) {
12 // do something with var1 and var2 here
13 }
14})To call another function in the same helper, use the syntax: this.methodName, where this is a reference to the helper itself. For example, helperMethod2 calls helperMethod3 with this code.
1this.helperMethod3(var1, var2);Creating a Helper
A helper resource is part of the component bundle and is auto-wired via the naming convention, <componentName>Helper.js.
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.
Using a Helper in a Controller
Add a helper argument to a controller function to enable the function to use the helper. Specify (component, event, helper) in the controller. These are standard parameters and you don't have to access them in the function.
This controller code calls an updateItem helper function.
1/* controller */
2({
3 newItemEvent: function(component, event, helper) {
4 helper.updateItem(component, event.getParam("item"));
5 }
6})Here’s the helper that contains the updateItem function called by the controller.
1/* helper */
2({
3 updateItem : function(component, item, callback) {
4 // Update the items via a server-side action
5 var action = component.get("c.saveItem");
6 action.setParams({"item" : item});
7 // Set any optional callback and enqueue the action
8 if (callback) {
9 action.setCallback(this, callback);
10 }
11 $A.enqueueAction(action);
12 }
13})The updateItem function accepts three parameters.
- component—The component to which the helper belongs.
- item—An item that’s set as an item parameter for the saveItem Apex action.
- callback—An optional callback to call after the saveItem Apex action returns. In our example, the newItemEvent controller method passes in only two arguments so there’s no callback.
Using a Helper in a Renderer
Add a helper argument to a renderer function to enable the function to use the helper. In the renderer, specify (component, helper) as parameters in a function signature to enable the function to access the component's helper. These are standard parameters and you don't have to access them in the function. The following code shows an example on how you can override the afterRender() function in the renderer and call open in the helper method.
detailsRenderer.js
1({
2 afterRender : function(component, helper){
3 helper.open(component, null, "new");
4 }
5})detailsHelper.js
1({
2 open : function(component, note, mode, sort){
3 if(mode === "new") {
4 //do something
5 }
6 // do something else, such as firing an event
7 }
8})