2

I have created a custom lightning component.

There is a corresponding js controller which has a init function which calls a helper function. Now, once an event is fired the same helper function is called from another controller method.

What I want to understand is is there any way to distinguish, when the helper function has been called from init or when it i called from event fired.

2
  • add a console.log ?
    – glls
    Aug 8, 2018 at 14:03
  • have you tried to console.log for event.getSource().get("v.name")
    – Amit Singh
    Aug 8, 2018 at 14:07

2 Answers 2

1

If it is strictly for troubleshooting, why can't you include a console.log() in the controller JS like below?

If you need the caller for your business logic, then you can pass a parameter to the helper function and use it to conditionally do stuff.

Also, not sure if you are aware: You can determine the Global Id and Name of the component in context from within JS. Please see below for how-to.

Init Controller Method

doInit : function(component, event, helper) {
        console.log('------------------> IN doInit NOW.');
        helper.thatSingleHelperMethod(component, event, helper, 'doInit');
    },

The Other Controller Method

someOtherControllerMethod : function(component, event, helper) {
        console.log('------------------> IN someOtherControllerMethod NOW.');
        helper.thatSingleHelperMethod(component, event, helper, 'someOtherControllerMethod');
    }

Helper

thatSingleHelperMethod : function(component, event, helper, source) {
        console.log('------------------> In thatSingleHelperMethod now.');
        console.log('------------------> Source = ' + source);
        var componentId = component.getGlobalId();
        var componentName = component.getName().substring(1);
        console.log('------------------> This Component Id = ' + componentId);
        console.log('------------------> This Component Name = ' + componentName);
        //Do Something based on source
    }
1

If all you need to do is specifically identify which event triggered the calling controller function, I would suggest using the event argument to inspect information about what triggered it.

I would look specifically at one of the following:

event.getName()
event.getSourceEvent()
event.getType() 

One of these should give you a deterministic way to identify the event that initiated this logic and then branch your logic accordingly.

The docs on this are pretty limited, but you can read a little about this part of the JS API for Lightning Components by appending /auradocs onto the end of your org's domain while logged in.

You must log in to answer this question.

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