Reputation: 88
I'm a newbie with Coffeescript, and I want to pass a function as an argument to be used as a callback when an animation is complete. Right now, my "callback" is being run before my animation completes.
closeItem: ($elem) ->
@close($elem, @myCallback($elem))
close: ($elem, callback) ->
$elem.slideUp 300, (-> callback)
Upvotes: 1
Views: 3821
Reputation: 48167
Your problem is that you are calling your function when you call close
. We are overloading terms here, but you seem like you want to "close" over the $elem
variable to make this happen. Try this:
closeItem: ($elem) ->
@close $elem, => @myCallback($elem)
close: ($elem, callback) ->
$elem.slideUp 300, callback
Notice a few things:
We are using the fat arrow =>
in the call to @close
. This creates a function with the context of the current object, so that it can call @callback
and it then closes upon $elem
so that it can be passed to @myCallback
Also, in the close
function itself, you just pass the callback along to slideUp
Upvotes: 4