ltng require scripts not working for me!

By | August 10, 2018

I'm having trouble accessing my static resource (a js lib) in my controller.js in my lightning component.

My goal is to bundle up all my javascript functions and classes and use across most components, and NOT paste them in the helper.js everytime!

in static resource (BaseFunctions / public / application/javascript)

window.BaseFunctions = (function() {
    var _toast = function (type, title, message, data) {
        let params = {
            'mode': 'dismissible',
            'duration': '5000'
        };
        if (type === 'info' || !type) {
            params.title = title || 'Info';
            params.key = 'info_alt'; // or info
            params.type = 'info';
        } else if (type === 'success') {
            params.title = title || 'Success';
            params.key = params.type = 'success';
        } else if (type === 'warning') {
            params.title = title || 'Warning';
            params.key = params.type = 'warning';
        } else if (type === 'error') {
            params.title = title || 'Error';
            params.key = params.type = 'error';
        }
        if (Array.isArray(data)) {
            params.messageTemplate = message;
            params.messageTemplateData = data;
        } else {
            params.message = message;
        }
        $A.get('e.force:showToast').setParams(params).fire();
    };
    var _refreshView = function () {
        $A.get('e.force:refreshView').fire();
    };
    var _closeQuickAction = function () {
        $A.get('e.force:closeQuickAction').fire();
    };
    return {
        toast: _toast,
        closeQuickAction: _closeQuickAction,
        refreshView: _refreshView
    };
}());

in .cmp file

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

in controller.js / init function

BaseFunctions // this is undefined!
// this obviously also crashes the component
BaseFunctions.toast('info', null, 'test message');