Is this the correct pattern to handle a button click? I want to reuse the code but it seems like I should be able to have a single method int he helper.

Currently I have to have two methods to reuse the code and is seems to me to be duplicative:


    showContent: function (component,event,helper) {




    showContent: function (component) {
        var toggleText = component.find("theContent");
        $A.util.toggleClass(toggleText, "noDisplay");



Is there any way to simply call the method in the helper from the button click?

<lightning:button class="slds-m-top--medium" variant="brand" label="Show 
Content" onclick="{!c.showContent}"/>

Removing the controller method results in an error..

1 Answer 1


You can't reference helper methods in Lightning merge fields, only controller methods. Helper functions should generally carry reusable functions. In your case, there's probably no reason to have the helper method, because it's simply two lines of code.

You use a helper method when it helps you keep you DRY (Don't Repeat Yourself) principles. For example, in my original pagination/sortable table Lightning component I had written, I wrote the following method:

// inside paginationSortingHelper.js
sortBy: function(component, field) {
    var sortAsc = component.get("v.sortAsc"),
        sortField = component.get("v.sortField"),
        records = component.get("v.allAccounts");
    sortAsc = sortField != field || !sortAsc;
        var t1 = a[field] == b[field],
            t2 = (!a[field] && b[field]) || (a[field] < b[field]);
        return t1? 0: (sortAsc?-1:1)*(t2?1:-1);
    component.set("v.sortAsc", sortAsc);
    component.set("v.sortField", field);
    component.set("v.allAccounts", records);

This method was called from each column header in the table:

// Inside paginationSortingController.js
sortByName: function(component, event, helper) {
    helper.sortBy(component, "Name");
sortByIndustry: function(component, event, helper) {
    helper.sortBy(component, "Industry");
sortByAnnualRevenue: function(component, event, helper) {
    helper.sortBy(component, "AnnualRevenue");

Note: I've learned more since then, so it's no longer used, but does serve as a practical example.

The point of all this is that I didn't have to copy the same convoluted code three times to perform the same action. I could use the same function from multiple controller methods to achieve the same results. If I wanted to tweak the algorithm, I only had to do so once instead of three times.

Of course, it does help to keep your controller relatively small, because it makes it easier to read, but I wouldn't arbitrarily make everything a helper method. Salesforce documentation seems to suggest that your "business logic" should be in helper functions, and that's largely true, but I'd quantify that with a condition that the function to be reused should serve multiple uses to support the DRY concept.

  • 1
    Thanks for this answer makes perfect sense.The helper method will be used in various spots to show/hide the content so maybe it would make more sense to put it in a helper if I pass in the name of the element to show/hide so it is more reusable....I feel like I am starting over again ugh....Give me a few years and I will be 1/10 where you are lol....
    – Eric
    Jan 6, 2017 at 1:06
  • @Eric It's been a long time to get here, ever since I wrote my first computer program 30 years ago or so. I'm almost envious of younger people, they seem to get up to speed a lot faster than I did.
    – sfdcfox
    Jan 6, 2017 at 1:11
  • When they speak in 140 characters or less I believe it is engrained in their minds to write compact / efficient code lol.....(does that makes sense cause it does in my head?)
    – Eric
    Jan 6, 2017 at 1:15
  • @Eric haha, I know the feeling. Twitter has changed a lot. I've been obsessed with optimization since I was a child... I literally taught myself machine code just so I could write more efficient algorithms.
    – sfdcfox
    Jan 6, 2017 at 1:16
  • 3
    @Eric Before DF17 is my goal, probably sooner. As well as foxlib, an open source utility package reminiscent of apex-lang.
    – sfdcfox
    Jan 6, 2017 at 1:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .