I am trying to work out the correct behavior to string multiple callbacks together.
class Person
move_head: ->
head.animate({
left: x,
},{
complete: @move_foot,
});
move_foot: ->
foot.animate({
left: x,
},{
complete: @move_arm,
});
move_arm: ->
arm.animate({
left: x,
},{
complete: something_else,
});
Now the issue is, head animates fine which calls foot. Foot also animates fine. The problem is that when foot is done, it does not animate the arm. I can't figure out what the problem could be. I have a guess though that it might have to do with a scoping issue.
Use the fat arrow
=>
, to bindthis
to current context:UPDATE: Here's a short explanation:
One of the biggest gotchas in javascript is
this
. In a method (such as yourmove_head
),this
is your instance of Person, if you call it asnew Person().move_head()
. But it doesn't have to be. For instance, if you call it usingnew Person().move_head.call(null)
,this
will benull
.But the biggest gotcha is that when you are inside a
function
, like acomplete
callback to a jQuery animate call,this
is no longer bound to your object! It is probably bound towindow
, but not for certain. So what happens in your case is that you give jQuery a reference to@move_foot
(or really,this.move_foot
). This call seems to work, in that your animation completes. But when jQuery calls that function, it will no longer know thatthis
is supposed to be your Person. So when you tell it to then go to@move_arm
, it will look for a methodmove_arm
on some other object (perhapswindow
).A common way to solve this is to save a reference to
this
before you make the call, likevar self = this
, and then, inside the callback, refer to that bound variableself
instead.You could use this technique, there is nothing wrong with it. Something like:
But CS does this (binding of
this
) for you, by giving you the fat arrow:=>
.The fat arrow says: "Everything inside this method referring to
this
, shall refer to this object."