I have several functions that I want to use in various Lightning components, and I don't want to copy and paste them into multiple Helpers, as they will quickly become difficult to maintain.
For example, I have the following as the utils
Static Resource:
(function(w) {
w.Utils = {};
w.Utils.getSix = function() {
return 6;
};
})(window);
I have a Lightning component, myComponent
, that includes:
<ltng:require scripts="{!$Resource.utils}" />
In myComponentController.js
, I am able to use the following:
({
"myControllerFunc": function() {
if (Utils.getSix() === 6) {
// do stuff
}
}
})
And this seems to be working fine when I test my app. However, when I installed the Lightning CLI to lint my code, I get errors
- From the
secure-window
rule stating thatUtils
is an invalid SecureWindow API, and - From the
no-undef
rule stating thatUtils
is not defined.
I would rather not be getting errors from the linter, because I want to ensure my code will continue to run in the future when Salesforce will undoubtedly change things.
How can I write code that can be used in multiple components while respecting the wishes of the LockerService?