0

I'm a js noob working on a Rails app. I defined a useful JS function in one of my javascript files, and I'd like it to be available to call in other files. However, it's not. Here's a simplified example of my situation... note that I'm using CoffeeScript (but AFAIK that should make no difference):

#file: app/assets/javascripts/foos.js.coffee
testFoo: ->
  alert 'Foo'

#file: app/assets/javascripts/bars.js.coffee
jQuery ->
  testFoo()

This doesn't work, and in the console I get: testFoo is not defined.

Both files are included in the page, if I put an alert or console log in either one the page responds as expected. I feel like I must be overlooking something brutally obvious here... what is it?

Andrew
  • 41,467
  • 48
  • 177
  • 278

1 Answers1

2

In short try this :

root = exports ? this
root.foo = -> alert 'Foo'
Mostafa Ali
  • 151
  • 4