1

I have a static resource to reuse javascript between my lightning components. I'd like the code to use specific credentials for an api in a sandbox, and another in production. I plan to use an apex controller function in my component to determine if it's being called in production first, the instantiate my static resource with with a returned value.

This is a sample of what my static resource looks like. It's similar to what can be found in the docs.

window.Utility = (function (isSandbox) {
    let name;
    let transactionKey;
    let endPoint;

    if (isSandbox) {
        name = "abc";
        transactionKey = "123";
        endPoint = "https://test.net/";
    } else {
        name = "def";
        transactionKey = "456";
        endPoint = "https://api.net/";
    }

    return { // public api 

        getData: function (recordId) {
            // do stuff
        }
    };
}());

When I call this code

    Utility(true).getData('5000v000003r3tEAAQ');

It doesn't work. I have a ton of functions in my static resource, and it would be ideal to set the class variables every time I call a function vs having to pass them via function.

0

1 Answer 1

2

Found out how to do this in this blog post. My problem was I had extra (). Oddly the static resource can still be called as long as I call it like so

Utility.getData('5000v000003r3tEAAQ');

The correct way to instantiate a javascript resource is to write your code like this:

window.Utility = function (isSandbox) {
    let name;
    let transactionKey;
    let endPoint;

    if (isSandbox) {
        name = "abc";
        transactionKey = "123";
        endPoint = "https://test.net/";
    } else {
        name = "def";
        transactionKey = "456";
        endPoint = "https://api.net/";
    }

    return { // public api 

        getData: function (recordId) {
            // do stuff
        }
    };
};

and call it like so in your aura component:

    let lib = new Utility(true);
    var result = lib.getData('5000v000003r3tEAAQ');

You must log in to answer this question.

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