In my Rails project, in which I am using Coffeescript, I want to be able to do something like this in a view:
f.text_area :content, :size => "77x4", :oninput => 'Helpers.expandTextarea(300)'
This would call a coffeescript function that would expand the textarea when required. My problem is that I can’t seem to properly namespace the coffeescript to get this to work. I tried to do it like this
# site.js.coffee
Helpers =
expandTextarea: ->
alert "function found!"
This doesn’t work. Trying to call Helpers in the browser console will throw an undefined error. My question is this:
What code would I use in site.js.coffee to achieve the namespacing I want?
You could manually put
Helpersinto the global namespace:Then you should be able to say
Helpers.expandTextarea(300)anywhere you want. Assuming, of course, thatsite.js.coffeeis included on every page.CoffeeScript files are wrapped in functions by default:
to avoid polluting the global namespace. You can supply options to the CoffeeScript compiler to not wrap like this but you’re better off explicitly putting global things into
window.