How to call a method in Lightning!?

The shortest way of calling a controller function from another controller function you can use:

$A.enqueueAction(component.get('c.controllerMethod'));

When you are calling a helper function from a controller function you can use:

helper.helperMethod(component, event, helper);

When you are calling a helper function from a helper function you can use:

this.helperMethod(component, event, helper);

However, if you are calling a helper function from a helper function, but within a callback method you need to do the following:

// Here we assign the value of this into the self variable
var self = this;

var action = component.get('c.serverSideControllerMethod');

action.setCallback(this, function(response){
    var state = response.getState();

    if(state == 'SUCCESS'){
        // Code here.

        // Here we trigger the helper method with the self variable previously defined
        self.helperMethod(component, event, helper);
        // If we call this.helperMethod(component, event, helper); here it will look at this in the context of the callback function NOT the helper function
    }
})

 

你可能感兴趣的:(salesforce)