-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathminivents.js
More file actions
29 lines (29 loc) · 820 Bytes
/
minivents.js
File metadata and controls
29 lines (29 loc) · 820 Bytes
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
function Events(target){
var events = {}, empty = [];
target = target || this
/**
* On: listen to events
*/
target.on = function(type, func, ctx){
(events[type] = events[type] || []).push([func, ctx])
return target
}
/**
* Off: stop listening to event / specific callback
*/
target.off = function(type, func){
type || (events = {})
var list = events[type] || empty,
i = list.length = func ? list.length : 0;
while(i--) func == list[i][0] && list.splice(i,1)
return target
}
/**
* Emit: send event, callbacks will be triggered
*/
target.emit = function(type){
var e = events[type] || empty, list = e.length > 0 ? e.slice(0, e.length) : e, i=0, j;
while(j=list[i++]) j[0].apply(j[1], empty.slice.call(arguments, 1))
return target
};
};