Created
February 3, 2014 10:03
-
-
Save ranbena/8781282 to your computer and use it in GitHub Desktop.
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: { | |
arr: {}, | |
bind: function(eventNamesArr, cb) { | |
!(eventNamesArr instanceof Array) && (eventNamesArr = [eventNamesArr]); | |
for (var idx in eventNamesArr) { | |
var eventName = eventNamesArr[idx]; | |
!(eventName in this.arr) && (this.arr[eventName] = []); | |
this.arr[eventName].push(cb); | |
} | |
}, | |
unbind: function(eventName, cb) { | |
if (!cb) { | |
this.arr[eventName] = {}; | |
} else { | |
for (var a = this.arr[eventName], i = a ? a.length - 1 : -1; i >= 0; --i) { | |
if (a[i] === cb) { | |
a.splice(i, 1); | |
return; | |
} | |
} | |
} | |
}, | |
trigger: function(eventName, data) { | |
if (eventName && eventName in this.arr) { | |
for (var i = 0, a = this.arr[eventName], len = a.length; i < len; i++) { | |
a[i](data); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment