First of all, I'm guessing that $("body").on "click", ".stuff", ->
is actually inside your initialize
method and you just have a formatting problem. Your code doesn't make a lot of sense otherwise.
The problem you'll run into is that jQuery controls what @
(AKA this
) is inside the callback function. So just binding functionB
won't be sufficient because you can't get at functionB
without having the right @
already.
In your specific case, none of this really matters because functionB
is defined as a bound function (using =>
) and you have no need for an anonymous wrapper in the event handler, just hand on
the function itself:
$("body").on "click", ".stuff", @functionB
If for some reason you insist on bind an anonymous function then you'd want to use _.bind
(or Function.prototype.bind
) rather than _.bindAll
; _.bindAll
binds functions (by name) to a specific @
but you want to find a specific function (which has no name) to a specific @
, see What is the difference between these Backbone/Underscore .bind() methods? for more discussion on the difference. You'd say something like:
$("body").on "click", ".stuff", _(-> @functionB()).bind(@)
# or
unbound_function = -> @functionB()
$("body").on "click", ".stuff", _.bind unbound_function, @