[SalesForce] Calling a static resource resource javascript functoin from lightning component

I have a static resource named as 'geoCode', which is a javascript file, containing a function. I want to call this function from lightning component controller and receive an alert message on screen.

function callMe() {
    alert('You are inside static resource');
}

Best Answer

You should be able to do this right inside of your controller code. However, you need to make sure that the call to this function doesn't happen until the script is loaded. This is done by specifying a controller action to be called in the afterScriptsLoaded parameter of <ltng:require>.

In your component:

<ltng:require scripts="/resource/geoCode" afterScriptsLoaded="{!c.scriptsLoaded}"/>

In your controller:

({
  scriptsLoaded : function(component, event, helper) {
    callMe();
  }
})

See this for the official docs: Using External JavaScript Libraries with Lightning Components