function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Peter Friberg 26Peter Friberg 26 

How to create reusable methods in Lightning JS client side controller for reuse within the client side controller itself

Normally when creating Javascript methods in Visualforce they exist in the same scope, e.g. you can call one method from another with parameters. Just as the two functions findNext() and findPrev() calls findAndFocus() here:
function findPrev() {
    search_index--;
    findAndFocus(text, search_index);
}

function findNext() {
    search_index++;
    findAndFocus(text, search_index);
}

function findAndFocus(str, stop_at) {
    // Do something
}
But when using Lightning how do you call one method from another?
({
    findPrev : function (component, event, helper) {
        search_index--;
        findAndFocus(text, search_index); // What to write here?
    },
    
    findNext : function (component, event, helper) {
        search_index++;
        findAndFocus(text, search_index); // What to write here?
    },

    findAndFocus : function(text, stop_at) {
        // Do something
    }
})
What is the best practice? Is it to move all methods for reuse into the helper client side Javascript file?
What if the helper file methods need to call other methods within the same helper Javascript file?

Any thoughts or ideas?
 
Best Answer chosen by Peter Friberg 26
Akhil AnilAkhil Anil
You need to place all the reusable code in the helper file, which you can call as "helper.functionname(component,event)". In case, if the helper method themselves need to call other methods, then you can call the same by using the "this" keyword. See the below helper file sample.
 
({
    findPrev : function (component, event, helper) {
        search_index--;
        this.findAndFocus(text, search_index); 
    },
    
    findNext : function (component, event, helper) {
        search_index++;
        this.findAndFocus(text, search_index); 
    },

    findAndFocus : function(text, stop_at) {
        // Do something
    }
})

Kindly mark this as a solution, if you find it useful.
 

All Answers

Akhil AnilAkhil Anil
You need to place all the reusable code in the helper file, which you can call as "helper.functionname(component,event)". In case, if the helper method themselves need to call other methods, then you can call the same by using the "this" keyword. See the below helper file sample.
 
({
    findPrev : function (component, event, helper) {
        search_index--;
        this.findAndFocus(text, search_index); 
    },
    
    findNext : function (component, event, helper) {
        search_index++;
        this.findAndFocus(text, search_index); 
    },

    findAndFocus : function(text, stop_at) {
        // Do something
    }
})

Kindly mark this as a solution, if you find it useful.
 
This was selected as the best answer
Peter Friberg 26Peter Friberg 26
Thanks Akhil.
I was close but was missing the this keyword :)
/Peter
Francesco Carmagnola 10Francesco Carmagnola 10
Hello, everything is right, but I have got a problem. The "this" keyword not work everytime for helper functions, "this" depends also on where the previous helper function was called. So, for example, I've got this code:
 
addItem : function(component) {
		var newItem = {};
		newItem = this.addLookups(component, newItem);
},

addLookups(component,newItem){
    //DO STUFF
}
The function "addItem" works well when the action is called by a button, but not on a "init" function for example (in this case, "this" is undefined)
 
Prasanta TrailheadPrasanta Trailhead
Yes, it is possible to call one controller method from other without havinga a helper.

https://salesforceblog.tumblr.com/post/178881975383/how-to-call-lightning-component-js-controller-call
narinder singh 37narinder singh 37
thanks for this valuable information. attitude status (https://yourstatus.in/attitude-status/), Good morning quotesfunny statuslove status.