-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventify.js
More file actions
32 lines (29 loc) · 1.13 KB
/
eventify.js
File metadata and controls
32 lines (29 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Eventify adds bind/trigger event functionality to any class! yay!
window.Eventify = function(c) {
c.prototype.bind = function(eventName, func) {
if(!this.event_bindings)
this.event_bindings = {}
if(!this.event_bindings[eventName])
this.event_bindings[eventName] = []
this.event_bindings[eventName].push(func)
}
c.prototype.trigger = function(eventName) {
if(!this.event_bindings) return;
if(!this.event_bindings[eventName]) return;
for(var i = 0; i < this.event_bindings[eventName].length; i++) {
this.event_bindings[eventName][i].apply(this, Array.prototype.slice.call(arguments, 1))
}
}
c.prototype.unbind = function(eventName, func) {
if(!this.event_bindings) return;
if(!this.event_bindings[eventName]) return;
if(!func) {
delete this.event_bindings[eventName]
return
}
for(var i = this.event_bindings[eventName].length - 1; i >= 0; i-- ) {
if(this.event_bindings[eventName][i] == func)
this.event_bindings[eventName].splice(i, 1)
}
}
}