9

In mulitple views I am including an external javascript library with this HTML:

<script type="text/javascript" 
        src="http://static.jstree.com/v.1.0pre/jquery.jstree.js">
</script>

In reading through the Rails 3.1 Asset Pipeline documentation and discussions I get the impression that vendor/assets/javascripts is a place were that file should be referenced. I'm guessing that I could download a copy of the file (jstree.js) and place it in that directory. However I'd like to have it loaded off the project site instead of making a local copy of it.

What do I put in vendor/assets/javascripts to pull a copy of jstree.js off the remote server? Do I create a .js file with some sort of remote load code? There seems to be all sorts of approaches and/or confusion on how to best do this ( see the long list of answers to this question: How do I include a JavaScript file in another JavaScript file? .)

Is there a "Rails Standard" convention/library/process for doing this? I'm a javascript neophyte, so please be explicit, thanks.

Community
  • 1
  • 1
Don Leatham
  • 2,694
  • 4
  • 29
  • 41

1 Answers1

10

No, there's no way you can put a "reference" to a remote file in the assets folder. You either download a copy and put into assets, or reference remote file with <script> tag.

Having a copy is not a bad idea, really. At least, you know that it works and the file hasn't changed (unless you changed it yourself). When you load a remote file, all sorts of surprises can happen :)

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 3
    Thanks. Wanted to make sure I wasn't missing something obvious and you confirmed that. Don't quite agree having a local copy is a good idea (except in production.) Libraries have learned to have "stable" versions that are never changed, so I'm not too concerned about that. If Rails supported a "reference" system (something like a Gemfile) I could more easily maintain my various JS libraries in dev/test/production. I would not have to manually download/organize files and could avoid the sprockets confusion. Would be super easy to dev/test against a new version of the remote library. – Don Leatham Mar 30 '12 at 18:14
  • 2
    I'm not sure it is always a good practice to store all of your JS files locally. Using third party services, like social buttons, it can be a lot of heart ache to debug if they change the service. – Mild Fuzz Dec 06 '12 at 11:58
  • +1 on a Gemfile for remotely stored JS files. @Don Could even include remotely downloading them per deploy, but locally serving them via sprockets. – Trip Oct 27 '14 at 16:06