0

I cannot seem to get a component to load even a basic static resource script when Locker Service is active in a Winter preview sandbox. It works fine if Locker Service is deactivated. Otherwise, I get the following error. What am I doing wrong?

Something has gone wrong. Action failed: c$LockerTestComp$controller$doInit [lockerTest is not defined] Failing descriptor: {c$LockerTestComp$controller$doInit}. Please try again.

LockerTest.vfp

<apex:page >
    <apex:includeLightning />
    <div id="lightning"></div>
    <script type="text/javascript">
    $Lightning.use("c:LockerTestApp", function() {
        $Lightning.createComponent('c:LockerTestComp', {}, "lightning", function(cmp) { });
    });
    </script>
</apex:page>

LockerTestApp.app

<aura:application extends="ltng:outApp" >
</aura:application>

LockerTestComp.cmp

<aura:component >
    <ltng:require scripts="{!$Resource.LockerTest}" afterScriptsLoaded="{!c.doInit}" />
    Hello World
</aura:component>

LockerTestCompController.js

({
    doInit : function(component, event, helper) {
        lockerTest();
    }
})

LockerTest.js (static resource)

var lockerTest = function() {
    alert('it worked');
}

1 Answer 1

3

var lockerTest= is not creating a global method - its a local variable not in the scope of the doInit method. Strict mode removes the creation of automatic globals and you simply need to indicate your intent is to actually create a globally visible function using window.lockertest=

Please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode for more details on strict mode and global variables.

You must log in to answer this question.

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