Question

I am in the process of converting an existing Rails 3.1 app I made for a client into a Backbone.js app with the Rails app only as a backend server extension. This is only a personal project of mine, to learn more about Backbone.js.

While setting up Backbone.js (using Backbone-on-Rails), I noticed I have some dependencies (like backbone-forms) that come from external sources and are frequently updated.

I've grown accustomed to using Bundler to manage my Ruby gems, but I haven't found anything similar for JavaScript files. I'm wondering if there is any way to do the same for Javascript (and possibly css) files.

Basically I can see three possibilities to solve this issue:

  • Simply write down all the sources for each JS file and check these sources from time to time to see what has changed.

  • Use some kind of existing "Bundler for Javascript" type of tool, I've been looking for something like this but have yet to find anything (good).

  • Since most of these JS files will be coming from Git anyway, use Git to get the files directly and use checkout to get the latest version from time to time.

I prefer the last option, but was hoping on some more input from other people who have gone this route or preferred some other way to tackle this issue (or is this even an issue?).

I figure the Git way seems easy, but I am not quite sure yet how I could make this work nicely with Rails 3.1 and Sprockets. I guess I'd try to checkout a single file using Git and have it be cloned in a directory that is accessible to Sprockets, but I haven't tried this yet.

Any thoughts?

Was it helpful?

Solution 2

For now I've settled on using the Git solution combined with some guard-shell magic.

The steps I follow:

  • Create a dependencies directory somewhere on your local drive
  • Clone the repositories with javascript (or css) files you want to use in the app
  • Set up a custom guard-shell command to do the following:
group 'dependencies' do
  guard 'shell' do
    dependencies = '~/path/to/dependencies/'

    watch(%r{backbone-forms/src/(backbone\-forms\.js)}) {|m| `cp #{dependencies + m[0]} vendor/assets/javascripts/#{m[1]}` }
  end
end
  • Place the Guardfile at the root of the app directory

It takes some time to set things up, but after that, when you have the Guard running, and you pull changes into your dependencies, the required files are automatically copied to your application directory, which are then part of your repository.

It seems to work great, you need to do some work for each new file you want to include in the asset pipeline, but all that is required is cloning the repository in your dependencies directory and adding a single line to your Guardfile, for example for the backbone-form css:

watch(%r{backbone-forms/src/(backbone\-forms\.css)}) {|m| `cp #{dependencies + m[0]} vendor/assets/stylesheets/#{m[1]}` }

Also, the reason I added this Guard to a group is because I keep my dependencies outside the main application directory, which means guard normally doesn't check my dependencies directory. To make this work, I start up my main Guard processes using bundle exec guard -g main and use bundle exec guard -w ~/path/to/dependencies -g dependencies in a new terminal window/tab to specify the -w(atchdir).

OTHER TIPS

You don't mention it in your alternatives, but ideally you should use something like Maven to manage your dependencies. Unfortunately, there are no public repositories for javascript files. This discussion lists some other options which might be of help to you: JQuery Availability on Maven Repositories

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top