I noticed that if I try to compile lines of Coffeescript like this:
$note.find('a.close').bind 'click', (event) =>
$(this).parent().remove()
The $(this) compiles to $(_this), which makes sense considering I’m using =>. The thing is, I don’t want to have to use -> in case I need to use local variables in this scope later.
Must I escape the second line with backticks to make $this compile correctly, à la:
`$(this).parent().remove()`
…or is there a better way?
Update (12 Jul 2012):
I ended up doing this:
close = -> $note.remove()
$note.find('a.close').bind 'click', (event) ->
close()
setTimeout close, duration
I know it avoids the use of this altogether, but that seems like a cleaner solution than using the same self = this trick that developing in plain JS often requires.
To get the element you clicked on, you can do:
—– or —–
But it’s not really answering your question, but it’s a good alternative.
I would use the first solution myself when possible.