How to load a js file for a specific route?

I am using fabric.js for an interactive canvas feature and would like to only load fabric.js for a particular route. I am using Iron Router and am currently loading the library in the route with:

IRLibLoader.load('https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js')

However, I’d like to pull in the library from the server for only this route so that I don’t have to depend on having internet access. Basically, I’d like to have fabric as a js file in my app and then only load it when I hit a specific route (and not use a CDN). Is there a way to do this?

I would just run some nginx or apache locally serving it if u r looking for fast solution to use it offline.

Put it in the public/ folder and use that path? i.e. public/fabric.js would become '/fabric.js' in terms of actual URL/URI for the browser.

@seeekr If I put it in the public folder it will automatically be requested when the app initially loads right? That is what I am trying to avoid.

There is probably a better way, but you could try something like this in your route controller with jQuery:

action: function() {
  var self = this;
  $.getScript('https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js', function(data, textStatus, jqxhr) {
    if(jqxhr.status === 200) {
      self.render();
    }
 });
}

Well, where would be the difference to putting it in client/ then? :wink:

“Be the change you want to see in the world.” (Mahatma Gandhi)