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); };