1

I have a js file(inside the folder of a component) that has a bunch of objects that hold properties on them. I want to access this file from an aura helper but I am not sure how to import/include it.

Do I do that in the component related to the helper? If that's the case how do I pass the js file in question to the controller of that component?

  • Where is this JS file now? A static resource? – sfdcfox Jan 5 '17 at 18:48
  • @sfdcfox It's related to this component so it's inside the folder of the component. So I have a folder and inside there're: component, controller, helper, jsFileInQuestio. – Arthlete Jan 5 '17 at 18:51
  • @sfdcfox Would it be a better idea to upload the file as a static resource then? If so how do I access that? Thank you, sorry for the lack of clarification on my side. – Arthlete Jan 5 '17 at 18:55
  • Nah, it's okay, give me a sec... – sfdcfox Jan 5 '17 at 18:55
  • Yeah, I can't find a way to reference stray files in the bundle. Go with the static resource. Answer's below. – sfdcfox Jan 5 '17 at 19:02
5

See Using External JavaScript Libraries. Basically, you can include the file, and then call a script after it loads. I've included the example from the manual below. As you can see, once imported, they are exposed as normal objects/methods. You don't need to do anything "special" to gain access to the objects, as they appear as part of the controller/helper's namespace.

Component

<ltng:require scripts="{!$Resource.chart}" 
              afterScriptsLoaded="{!c.setup}"/>
<canvas aura:id="chart" id="myChart" width="400" height="400"/>

Controller

setup : function(component, event, helper) {
    var data = {
        labels: ["January", "February", "March"],
        datasets: [{
            data: [65, 59, 80, 81, 56, 55, 40]
        }]
    };
    var el = component.find("chart").getElement();
    var ctx = el.getContext("2d");
    var myNewChart = new Chart(ctx).Line(data);
}

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.