The rails asset pipeline has it's issues, but the benefits of concatenating all my JS, minifying it and serving it with far-future expires headers are hard to ignore.
Lots of the JS in my rails app is specific to a single action. For example, we have a complex page for staff to enter customer orders.
pre rails 3.1, I had the action-specific code in a distinct JS file that was only loaded when needed. Now all my JS is served all the time. What's the best way to only run the order-entry JS when it's needed?
Currently I'm checking for the order-entry DOM elements, but that means there'll be lots of unnecessary functions running on DOMready.
Here's a snippet of coffeespcript from the order-entry code, and this pattern is repeated in about 20 files. Is there a better way?
$ ->
window.app.draft = new app.DraftOrder()
@module 'app', ->
class @DraftOrder
constructor: ->
@items = $('table.draft-items tr')
return if @items.size() == 0
@initEvents()
@move_first()
initEvents: ->
# foo
otherMethod: ->
# bar