-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass.eventdriver.coffee
More file actions
43 lines (31 loc) · 936 Bytes
/
class.eventdriver.coffee
File metadata and controls
43 lines (31 loc) · 936 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
###
EventDriver - Richard Lyon, Feb 24 2011
A simple class to allow jQuery-like trigger+bind events
For example:
a = new EventDriver; (or an object that extends it, as intended)
b = (function(x,y) { console.log(this,x,y) } )
a.bind( 'yoyo', b )
a.trigger( 'yoyo', 3.1412, 42 )
Results in "[Object EventDriver], 3.1412, 42" being logged (or something similar)
###
class EventDriver
events: {}
contexts: {}
bind: ( event, fn, context ) ->
if not @bound( event )
@events[event] = []
@contexts[event] = []
@events[event].push( fn )
@contexts[event].push( context )
unbind: (event) ->
@events[event] = []
@contexts[event] = []
trigger: (event, args...) ->
if @bound( event )
for i, f of @events[event]
do (i,f) =>
@contexts[event][i] ?= this
f.apply( @contexts[event][i], args )
bound: (event) ->
return @events[event]? #! return self.events.has(event)
this.EventDriver = EventDriver