Here is my CoffeeScript:
jQuery ->
$("form").submit (e) ->
e.preventDefault()
email = $("#email").val()
return if email.length == 0
$.ajax
url: "https://api.kickofflabs.com/v1/1905/subscribe",
data: "email=#{email}",
dataType: 'jsonp',
jsonp: 'jsonp',
jsonpCallback: 'subscribe_callback',
timeout: 2000,
error: (a, b, e) ->
alert e
console.log e
subscribe_callback = (data) ->
console.log(data)
alert("Signed up #{data.email}")
Here is a gist as well: https://gist.github.com/1630460
The only way I could get it work, was to move the callback outside of the coffeescript ‘wrap’.
My guess is the the callback can not be accessed because of the wrap. Is there a smart way to work around this?
In standard coffeescript scope the
thiskeyword refers to the global window object. So if you write your function asThen it should be the same as putting it outside of the closure. This is because the closure is invoked with
thisas a parameter: