1

I have written a helper class that I will be using throughout my Aura app. I have created a static resource file where I have added my auraUtils.js file.

In one of my components I am doing a doInit() call to retrieve data via an Apex class.

For everyone's benefit I am pasting my helper class below.

I am getting an undefined (auraUtils is undefined) message because (I presume) the component for doInit is not yet initialised. I have however placed my ltng:require ...auraUtils.js file into the app and other parent components.

Does anybody know of an elegant way of achieving this global helper class without having to paste the snippet into every component helper class?

Any help would be much appreciated as I am new to lightning.

var auraUtils = auraUtils || {}; 
auraUtils.getCompletedJobs = function(cmp, successCB, errorCB){
    auraUtils._createApexAuraCall(cmp, 'c.getCompletedJobs', null, successCB, errorCB);
};

auraUtils.getJobsByType = function(cmp, paramsMap, successCB, errorCB){
    auraUtils._createApexAuraCall(cmp, 'c.getJobsByType', paramsMap, successCB, errorCB);
};
auraUtils.getInProgressJobsByType = function(cmp, paramsMap, successCB, errorCB){
    auraUtils._createApexAuraCall(cmp, 'c.getInProgressJobsByType', paramsMap, successCB, errorCB);
};


// Private Methods
auraUtils._createApexAuraCall = function(cmp, name, paramsMap, successCB, errorCB){
    var a = cmp.get(name);
    if(paramsMap && paramsMap!=null){
        a.setParams(paramsMap);
    }
    if(!cmp || !cmp.isValid()){
        console.error('Component variable was not valid', cmp);
        return false;
    }
    a.setCallback(this, function(response) {
        var state = response.getState();
        if (state === 'SUCCESS') {
            if(successCB)successCB(response);
        }
        else if (state === 'INCOMPLETE') {
            alert("The server did not return a response. The server might be down or the client might be offline.");
            if(errorCB)errorCB();
        }
        else if (state === 'ERROR') {
            var errors = response.getError();
            if (errors) {
                if (errors[0] && errors[0].message) {
                    console.error("Error message: " + errors[0].message);
                    if(errorCB)errorCB();
                }
            } else {
                console.error("Unknown error");
                if(errorCB)errorCB();
            }
        }
    });
    $A.enqueueAction(a);
};
0

you can make the JS file a static resource and invoke the file on load of your component

AURA

 <ltng:require scripts="{!$Resource.auraJSUtil}"/>

JS

    myMethod : function(cmp, event, helper){
        getCompletedJobs(cmp, event, helper)
    }

https://developer.salesforce.com/docs/component-library/bundle/ltng:require

You might have to refactor the JS methods to use base JS instead of the lightning version of method instantiation

STATIC JS FILE

getCompletedJobs = function(cmp, event, helper) {
    //your code here
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.