Very similar to this question, I'm curious about common patterns/best practices for handling events in coffeescript classes, particularly when you also need to call the event handlers immediately to initialize state.
Unlike the answer to the question above though, let's assume though that you do need/want a class.
Here's a contrived example:
class @Foo
constructor ->
@bar = new Bar()
@checkbox = $('#my-checkbox')
# set up the events
@checkbox.click =>
@bar.clickHandler(@checkbox)
@clickHandler(@checkbox)
# initialize state
@bar.clickHandler(@checkbox)
@clickHandler(@checkbox)
clickHandler: ($elt) ->
if $elt.is(':checked')
# something
else
# something else
$ ->
window.foo = Foo.new
Is there a usual pattern for cases like this where you want the event function called both when the event fires and when the class loads? I really hate the duplication but I'm not sure how to remove it. Any other suggestions?