Breaking Up Your Asset Pipeline
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.
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.