Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling functions defined with coffee script?

I have the following coffeescript code to generate and alert box:

show_alert = () ->
  alert("Hello! I am an alert box!")

which compiles to:

(function() {
  var show_alert;

  show_alert = function() {
    return alert("Hello! I am an alert box!");
  };

}).call(this);

in my html I have the following

<input onclick='show_alert()' type='button' value='Show alert box' />

However, no alert box shows? The following is the html copied from the browser:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Test Rails Application</title>
    <style type='text/css'>.application h1 {
  color: lime; }
</style>
    <script type='text/javascript'>(function() {
  var show_alert;

  show_alert = function() {
    return alert("Hello! I am an alert box!");
  };

}).call(this);
</script>
  </head>
  <body>
    <h1>Hello from applicaiton.html.haml</h1>
    <div class='application'><h1>Hello World</h1>
<input onclick='show_alert()' type='button' value='Show alert box' />
</div>
  </body>
</html>

Why am I not able to get an alert box to show up?

like image 857
rudolph9 Avatar asked May 05 '12 04:05

rudolph9


People also ask

How do you call a function in CoffeeScript?

You can simply invoke a function by placing parenthesis after its name as shown in the following example. // Generated by CoffeeScript 1.10. 0 (function() { var add; add = function() { var a, b, c; a = 20; b = 30; c = a + b; return console. log("Sum of the two numbers is: " + c); }; add(); }).

How do you define a function in a script?

How to Define a Function. To create a function you define its name, any values ("arguments"), and some statements: function myfunction(argument1,argument2,etc) { some statements } A function with no arguments must include the parentheses: function myfunction() { some statements }

Is CoffeeScript still used?

As of today, January 2020, CoffeeScript is completely dead on the market (though the GitHub repository is still kind of alive).


3 Answers

Your problem is that the generated javascript code is in another scope. You have to solve this by either adding the -b parameter to the coffeescript compiler or export your function explicitly via

root = exports ? this
root.show_alert = () -> alert("Hello! I am an alert box!")

For more information about the exporting and scope issue have a look at https://stackoverflow.com/a/4215132/832273

I created a working jsfiddle with the above coffeescript code

like image 91
Ulrich Dangel Avatar answered Oct 16 '22 17:10

Ulrich Dangel


I found two ways to solve this issue FIRST add @ before function name

@say_hi = () ->
  $(alert('Hello!!!'))

SECOND at the end of coffee file add

window["say_hi"] = say_hi
like image 23
kapellan Avatar answered Oct 16 '22 18:10

kapellan


in your coffeescrpt code, try to save the function to window: window["show_alert"] = show_alert

like image 4
akonsu Avatar answered Oct 16 '22 17:10

akonsu