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?

5
  • Where is this JS file now? A static resource?
    – sfdcfox
    Commented Jan 5, 2017 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
    Commented Jan 5, 2017 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
    Commented Jan 5, 2017 at 18:55
  • Nah, it's okay, give me a sec...
    – sfdcfox
    Commented Jan 5, 2017 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
    Commented Jan 5, 2017 at 19:02

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .