Skip to content

Instantly share code, notes, and snippets.

@guenin
Created August 24, 2011 16:10
Show Gist options
  • Save guenin/1168415 to your computer and use it in GitHub Desktop.
Save guenin/1168415 to your computer and use it in GitHub Desktop.
jQuery Event love
# Event love!
Pinger =
ping: ->
console.log 'ping!'
$(this).trigger('pinged')
Ponger =
pong: ->
console.log 'pong!'
$(Pinger).bind 'pinged', Ponger.pong
Pinger.ping()
# ping!
# pong!
# Use objects! Pass objects!
class FoodStuff
constructor: (@food) ->
chunkify: ->
console.log "chunky #{@food}!"
$(this).trigger("chunkified", @food)
bacon = new FoodStuff("bacon")
$(bacon).bind "chunkified", (event, food) ->
console.log "yum yum #{food}"
bacon.chunkify()
# chunkified bacon!
# yum yum bacon
# Don't name your events the same as methods or IE goes into a recursive death loop
IE =
die: () ->
$(this).trigger('die')
IE.die()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment