Lightning

Helper in Lightning Bundle

Helper in lightning bundle is similar to controller where you write client-side javascript methods. These methods are more like reuseable methods which can be called from any controller method.

  • Helper is similar to controller but helper does not handle events.
  • Helper can be used to write reusable Javascript methods and these can be called from Controller section.
  • Helper functions also enable specialization of tasks, such as processing data and queueing server-side actions.
  • Helper methods are local to Component bundle.
  • Helper methods can be called from any controller javascript methods.
  • Declaring a helper method is similar to controller javascript method.
  • 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.
  • You can call a helper method from another helper method using this. operation.

Example:

({
    helperMethod1 : function() {
        // logic here
    },

    helperMethod2 : function(component) {
        // logic here
        this.helperMethod3(var1, var2);
    },

     helperMethod3 : function(var1, var2) {
         // do something with var1 and var2 here
    }
})

If you observe, helperMethod2 is calling helperMethod3 function as this.helperMethod3().

How can you call a helper method from controller?

If you see controller section which has 3 parameters, component, event, helper. Using helper component reference you can call a helper method.

If reference is helper, then

helper.helperMethod1();
helper.helperMethod2(component);
helper.helperMethod3(var1,var2);

Note:

A helper is in lightning bundle is saved as componentNameHelper.js. If you component name is My_Lightning_Component, then a controller will be saved as My_Lightning_ComponentHelper.js.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s