Call another method from a method in same Lightning controller

Given two controller methods defined for a Lightning component as follows, how can I call bar() from foo()? If I try it using the code shown below, I get the error, “bar is not defined”.

({
    bar : function(component, event, helper) {
        console.log('bar just happened');
    },
    foo : function(component, event, helper) {

        // I would love for this to work,
        // but it doesn't work as expected.
        bar(component, event, helper);

        // I've also tried this but get a slightly 
        // different but equally discouraging error.
        this.bar(component, event, helper);
    }
})

Answer

try this, it works perfectly

({
    bar : function(component, event, helper) {
        console.log('bar just happened');
    },
    foo : function(component, event, helper) {

        // I would love for this to work,
        // but it doesn't work as expected.
        //bar(component, event, helper);

        // I've also tried this but get a slightly 
        // different but equally discouraging error.
        //this.bar(component, event, helper);

        var a = component.get('c.bar');
        $A.enqueueAction(a);


    }
})

Attribution
Source : Link , Question Author : Marty C. , Answer Author : Renu Kumar

Leave a Comment