To accomplish this, Ryan Funduk and I recently came up with this approach. First we will leverage the Rails asset pipeline to create a json file that contains all of our app’s views.
1 2 3 4 5 6 7 8 9 10 11
Now, if you go to http://localhost:3000/assets/all_views.json you should see all your views in one large json object. Note that your Batman views will be processed through ERB, so if you need the server to do some processing, go right ahead. (Just remember that the ERB code will be run only once, when the views are compiled, not each time they are requested.)
Next, tell the Rails asset pipeline to precompile this file, like so:
Next, we write a Batman helper that will request the all_views.json file, and create Batman views for each of the individual views. That looks like this:
1 2 3 4 5 6 7 8
Note that we run this file through ERB first, so that we get the proper path (including the digest, if applicable) to the all_views file. The last line pre-populates the view store with each of the views. (You will need to be on the master Batmanjs branch, as the View store is a new addition since the 0.8 release).
The last thing we need to do is kick off the preloader, and a good place to do that is like so:
1 2 3 4
So, putting it all together, when your Rails app is deployed and the assets are precompiled, the asset pipeline will write out an all_views-1234.json file to the public/assets directory. On the client side of things, when your Batman app starts, it calls the preloader, which loads the all_views-1234.json file and creates Batman views for them all in one fell swoop. The really cool thing about this setup is that the Rails asset pipeline takes care of caching the views for you. Winning!
UPDATE: Andrew Bennett came up with an even nicer way to grab the view source using the asset pipeline, which will let you use HAML or ERB and also any Rails view helpers:
1 2 3 4 5 6 7 8 9 10 11 12