-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-mixin.js
63 lines (62 loc) · 2.09 KB
/
event-mixin.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var eventMixin = {
on : function ( event, callback ) {
if ( typeof callback !== 'function' ) {
throw "callback not function";
}
this.events()[ event ] = this.events()[ event ] || [];
if ( this.events()[ event ] ) {
this.events()[ event ].push( callback );
}
return this;
},
off : function ( event, callback ) {
if ( !callback ) {
delete this.events()[ event ];
} else {
if ( this.events()[ event ] ) {
var listeners = this.events()[ event ];
for ( var i = listeners.length-1; i>=0; --i ){
if ( listeners[ i ] === callback ) {
listeners.splice( i, 1 );
}
}
}
}
return this;
},
// Call the event with name, calling handlers with all other arguments
// e.g. trigger( name, ... )
trigger : function ( name, data ) {
var args = Array.prototype.slice.call( arguments, 1 );
var listeners;
var length;
// Find match listeners
if ( name !== "*" && this.events()[ name ] ) {
listeners = this.events()[ name ];
length = listeners.length;
while ( length-- ) {
if ( typeof listeners[ length ] === 'function' ) {
listeners[ length ].apply( this, args );
}
}
}
// Send to any listeners bound to '*'
if ( this.events()[ "*" ] ) {
listeners = this.events()[ "*" ];
length = listeners.length;
// Add the event name to the first callback arg
args.unshift( name );
while ( length-- ) {
if ( typeof listeners[ length ] === 'function' ) {
listeners[ length ].apply( this, args );
}
}
}
return this;
},
// Added function to avoid the use of a constuctor
events : function () {
this.eventsArray = this.eventsArray || [];
return this.eventsArray;
}
};