Created
April 14, 2012 05:46
-
-
Save ryanflorence/2382307 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@events = | |
events: {} | |
on: (topic, handler, context = this) -> | |
(@events[topic] or= []).push {handler, context} | |
trigger: (topic, args...) -> | |
return unless @events[topic]? | |
handler.apply(context, args) for {handler, context} in @events[topic] |
This file contains hidden or 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
(function() { | |
var slice = Array.prototype.slice; | |
this.events = { | |
events: {}, | |
on: function (topic, handler, context) { | |
if (context == null) context = this; | |
this.events[topic] = this.events[topic] || [] | |
this.events[topic].push({ handler: handler, context: context }); | |
}, | |
trigger: function (topic) { | |
if (this.events[topic] == null) return; | |
var args = slice.apply(arguments, 1); | |
for (var i = 0, l = this.events[topic].length, event; i < l; i++) { | |
event = this.events[topic][i]; | |
event.handler.apply(event.context, args); | |
} | |
} | |
}; | |
}).call(this); |
Yeah, I'm well aware of implicit returns guys; I'd return this
if I cared, but don't :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Something like this would be more correct (like source JS code)