Overview
When building Lightning (Aura) components, you often need to reuse client-side logic across controller methods. There are two common, safe ways to call a JS controller method from another JS controller method:
- Preferred: move reusable logic into the
helper.jsand call the helper method from controllers. - Direct call (less recommended): invoke another controller function using
this.methodName(...)from within the same controller object.
Why prefer helper methods?
Helpers provide a single place for shared logic, easier unit testing, and better separation of concerns. Controller methods should remain thin — handling events and delegating the heavy lifting to the helper.
Examples
Below are concise examples showing both approaches.
1) Recommended: Use helper methods
// controller.js
({
doInit : function(component, event, helper) {
// delegate work to helper
helper.loadData(component);
},
handleClick : function(component, event, helper) {
// reuse the same helper method
helper.loadData(component);
}
})
// helper.js
({
loadData : function(component) {
var action = component.get("c.getServerData");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
component.set("v.records", response.getReturnValue());
}
});
$A.enqueueAction(action);
}
})
2) Direct call from one controller method to another
You can call another controller method using this.methodName(component, event, helper) because controller methods are properties of the same object literal. Use this approach sparingly — helper is usually cleaner.
// controller.js
({
methodOne : function(component, event, helper) {
// do some work
// call another controller method in the same file
this.methodTwo(component, event, helper);
},
methodTwo : function(component, event, helper) {
// logic reused by methodOne
console.log('methodTwo executed');
}
})
Notes and gotchas
- If you try to call a controller method from outside the controller (for example, from renderer or another JS file),
thismay not resolve to the controller object — prefer helper or usecomponent.getEvent/aura:method/ events for cross-component communication. - For parent-child communication use
aura:method(expose methods on child components) or application/component events instead of direct JS calls. - Arrow functions can change
thisbehavior. Avoid using arrow functions for controller methods; use standard function syntax.
Quick recommendation
Always prefer helpers for shared client-side logic. Use this.methodName only when the logic is small and tightly scoped to the controller file.






Leave a Reply