Under Rails 3.1, I’m trying to find out how to move a few coffeescript classes away from my controller default coffeescript file (home.js.coffee) into another file, in order to structure the whole a little bit.
Does anyone know how to “include” a coffeescript file into another?
What you want to do is export functionality. For instance, if you start with
and you decide you move
Footo its own file, that file should look like(where
window.Foo = FoomakesFooa global), andBar‘s file should start with the Sprockets directive(assuming that you’ve named
Foo‘s fileFoo.js.coffee). Each file is compiled into JS independently, but Sprockets will ensure thatFoois included beforeBar.Note that, as a shortcut, you can get rid of the
window.Foo = Fooline, and instead writeor simply
to define a class named
Foothat’s attached to thewindowobject.