24

In my CoffeeScript file, clients.js.coffee,

myFunction = (variable) ->

I created a function in CoffeeScript in app/assets/javascript. But when I try to call that function, the console shows me an error saying function not found.

I check the source of the page and it shows that the script is loaded:

<script src="/assets/clients.js?body=1" type="text/javascript"></script>

This is what was found inside the script source:

(function() {
  var myFunction;

  myFunction = function(variable) {}
}).call(this);

Any idea what am I missing? What should I do to call the function?

Jeremy
  • 1
  • 85
  • 340
  • 366
revolver
  • 2,385
  • 5
  • 24
  • 40

2 Answers2

59

To make it accessible from outside, all you need to do is add an '@' in front. This will attach the function to the window object.

@myFunction = (variable) ->
SMathew
  • 3,993
  • 1
  • 18
  • 10
7

Bind it to the window

myFunction = (variable) ->
  alert('zzzzzzzz')

window.myFunction = myFunction
house9
  • 20,359
  • 8
  • 55
  • 61
  • Also here is a screencast which might be helpful - http://house9.blogspot.com/2011/05/rails-31-javascript-execution.html – house9 Jul 13 '12 at 04:35