13

Someone know how can I use this option in Rails 3.1? Now CoffeScript puts a function with .call(this) on each file, but I want to remove this.

EDIT: "Can't find variable” error with Rails 3.1 and Coffeescript" and "Pattern for CoffeeScript modules" have what I want. I'll change my global vars to use @global scope.

Community
  • 1
  • 1
Edison Machado
  • 1,410
  • 19
  • 29

2 Answers2

20

I'd recommend against doing this. See my answer at Pattern for CoffeeScript modules for some of the reasons why. ("Making your CoffeeScript code incompatible with out-of-the-box Rails 3.1" is yet another reason.) Better to just use

window.a = b

or even

@a = b

instead of a = b when you're trying to export something to global scope.

In previous versions of Rails 3.1, bare compilation was enabled. This was classified as a bug, and fixed in RC1.

So while I strongly encourage you not to do this, here's how to turn bare compilation back on: Add

Tilt::CoffeeScriptTemplate.default_bare = true

to your environment.rb.

Community
  • 1
  • 1
Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
  • Yes, I agree. Im starting a new project, so I want to begin from the right way, I'll follow your strategy. Thanks :) – Edison Machado May 23 '11 at 16:26
  • 1
    I agree if you have only some coffeescript files, but if you use a coffeescript file for each class/object you make you don't want to define a global variable for each one. +1 anyway for the answer – makevoid Sep 14 '11 at 15:29
4

I do recommend taking advantage of CoffeeScript's closures and following a CommonJS module patter. But sometimes, just sometimes, it is OK to want to use the --bare option. In my case, when rendering a Jasmine spec helper so I could keep things at the top level and also take advantage of the include Sprockets directive in said Jasmine specs.

To that end, I created the "sprockets-blackcoffee" gem, which you can learn about here. https://github.com/metaskills/sprockets-blackcoffee

MetaSkills
  • 1,954
  • 18
  • 15
  • 1
    I now realize that everything depends on the point of view and the kind of project. As in your case, I agree. :) – Edison Machado Jan 13 '12 at 03:45
  • Very nifty. Your stuff has been cropping up a lot in the last few days, as I use minitest and CoffeeScript in Rails 3.2... Cheers! :-) – Graham Ashton Feb 29 '12 at 21:41