Coffee Script & Rails 3.1

Are there any examples on how to use coffee script in your rails app. I don’t know how people are using coffee script. I know the mechanics of how to write the coffee script. But how do I tie it to my views / application?

Here is the very condensed form how I am doing it:

Each rendered view contains a class “controller_nameofcontroller” and “controllername_actionname” attached to the body element

In my scripts I have the following scenarios ( I am using backbone, but this works as well without it):

  1. Applies to everything: Generic startup and configuration, for example for colorbox. I run a

class AppView extends Backbone.View

el: $('#outercontainer')

initialize: ->

	$('.token_input_cont').each (i,x) ->

		$(x).tokenInput  $(x).data("source"),

			preventDuplicates : true

			prePopulate: $(x).data("pre")

			theme: $(x).data("theme") || ''

$ →

window.App = new AppView

and make sure that the init code retrieves it’s processing instructions from the tags as shown in the above example. This way

you can control a lot of functionality through markup.

  1. To have functionality targeted at specific pages I use this:

$ →

window.HomeIndexView = new HomeIndexView if $('body.home_index').length > 0

where I check for the existence of the tag.

Hope this helps a bit