Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a jQuery function with two function arguments into Coffeescript [duplicate]

I want to convert a jQuery function written in JavaScript into Coffeescript, which gets two functions as parameters. I'm new to Coffeescript and I'm a little stuck here. The original function is something like:

$('#target').toggle(function() {
  alert('First handler for .toggle() called.');
}, function() {
  alert('Second handler for .toggle() called.');
});

How does it look like in Coffeescript?

like image 801
acme Avatar asked Dec 07 '22 19:12

acme


1 Answers

An easy way to see how something works in coffeescript in real time is to go over to the official website and use the "Try Coffeescript" dropdown pane. One way I found of getting it to output the exact same code as what you wanted was to do this:

$('#target').toggle ->
  alert 'First handler for .toggle() called.'
, ->
  alert 'Second handler for .toggle() called.'

Give it a try. It may feel a bit odd typing code into a website, but I've certainly found it useful.

like image 105
genericdave Avatar answered Dec 10 '22 12:12

genericdave