Skip to content

Instantly share code, notes, and snippets.

Last active March 6, 2016 14:54
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Coffee Arrows
class SomeAjaxCallback
constructor: (@value) ->
throw "Bad value" unless value?
setTimeout @process, 5000
# setTimeout doesn't give us a "new context"
# so we'd get our "global context" : window.
# let's avoid that using the fat arrow.
process: =>
throw "Wrong context" unless @value?
class SomeElement
constructor: (@value, sel) ->
# now, we have another problem.
# We need to keep our `this`, but
# jQuery gives us a new one through the event.
# Thankfully, we can use the parameter (ev)
# to get the target, and still use the fat arrow.
$(sel).click @onclick
onclick: (ev) =>
$( @value
class NotNiceApi
constructor: (@value) ->
# now, the third case :
# the library doesn't pass the new `this`
# as an argument. We have to bind
# the function by ourselves.
self = this
onmsg = ->
@dispatch 'new value', self.value
SomeApi.on 'message', onmsg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment