The Rails Asset Pipeline to me is kind of like Bundler. At first I was very nervous about it and thought that it would be very troublesome. But after a while of using it, I realized the wisdom behind it and my life got a lot easier. The asset pipeline is fabulous for putting all your assets into a single file, compressing them, and serving them in your site without cluttering everything up. Remember these days?
A basic component of the Asset Pipeline is the manifest file. A manifest looks something like this
For a quick rundown on how the Asset Pipeline works, I highly recommend taking a few minutes to watch Railscast episode 279. But there are a few things that I'd like to point out here.
First, notice that the name of the file is
application.js. In production, if specified, it will also be compressed and uglified. So rather than specifying a really long and ugly
application.js file looks just like the one above but I removed the
application.js. I just want some of the big stuff that I use all over like jQuery. (Ok, I also added the underscore library in there too because it's just so darn useful!) This file is loaded in the head of my layout just as I described above.
Then, I created another manifest file named
load_my_app.js in the root of
my_single_page_app/. It looks like this:
//= require modernizr //= require backbone-min //= require Backbone.ModelBinder //= require my_single_page_app/my_app //= require_tree ./my_single_page_app/templates //= require_tree ./my_single_page_app/models //= require_tree ./my_single_page_app/collections //= require_tree ./my_single_page_app/views //= require_tree ./my_single_page_app/utils
Then in my view that displays the single page app, I have these lines:
Now my single page application is loaded into the page as
my_single_page_app/load_my_app.js by the Asset Pipeline and it's only loaded when needed. And a big bonus is that I don't need to worry about any of the code that I wrote or libraries I want to use interfering with the rest of the site.