Created
August 24, 2011 16:10
-
-
Save guenin/1168415 to your computer and use it in GitHub Desktop.
jQuery Event love
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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