0

I've done some searching on this, but I cannot find info. I'm building an application inside sinatra, and using the coffeescript templating engine. By default the compiled code is wrapped as such:

(function() {
  // code
}).call(this);

I'd like to remove that using the --bare flag, so different files can access classes and so forth that I'm defining. I realize that having it more contained helps against variable conflicts and so forth, but I'm working on two main pieces here. One is the business logic, and arrangement of data in class structures. The other is the view functionality using raphaeljs. I would prefer to keep these two pieces in separate files. Since the two files wrapped as such cannot access the data, it obviously won't work. However, if you can think of a better solution than using the --bare option, I'm all ears.

agmcleod
  • 13,321
  • 13
  • 57
  • 96

2 Answers2

2

Bare compilation is simply a bad practice. Each file should export to the global scope only the public objects that matter to the rest of your app.

# foo.coffee
class Foo
  constructor: (@abc) ->

privateVar = 123

window.Foo = Foo # export

Foo is now globally available. Now if that pattern isn't practical, maybe you should rethink your structure a bit. If you have to export too may things, you nest and namespace things better, so that more data can be exposed through fewer global variables.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • +1 to this. There's nothing exceptional about the scenario described in the question; when you want an object to be visible from multiple `.coffee` files, you put it in a global namespace. – Trevor Burnham Apr 14 '12 at 01:52
1

I support Alex's answer, but if you absolutely must do this, I believe my answer to the same question for Rails 3.1 is applicable here as well: Put the line

Tilt::CoffeeScriptTemplate.default_bare = true

somewhere in your application.

Community
  • 1
  • 1
Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196