Repository navigation
Expand file tree
/
Copy pathevented.html
More file actions
83 lines (59 loc) · 3.4 KB
/
Copy pathevented.html
File metadata and controls
83 lines (59 loc) · 3.4 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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html>
<html>
<head>
<title>evented.coffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>evented.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<h3 id="evented">Evented</h3>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Modified from <a href="https://gist.github.com/contra/2759355">https://gist.github.com/contra/2759355</a>.</p>
</div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Instances of Model emit <code>'step'</code> and <code>'draw'</code> events so that an arbitrary
function can be triggered every time the model is stepped or drawn (e.g. the patch inspector).</p>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Evented</span></span>
<span class="hljs-attribute">constructor</span>: <span class="hljs-function"><span class="hljs-params">()</span> -></span>
<span class="hljs-property">@events</span> = {}
<span class="hljs-attribute">emit</span>: <span class="hljs-function"><span class="hljs-params">(name, args...)</span> -></span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@events</span>[name]
cb args... <span class="hljs-keyword">for</span> cb <span class="hljs-keyword">in</span> <span class="hljs-property">@events</span>[name]
<span class="hljs-attribute">on</span>: <span class="hljs-function"><span class="hljs-params">(name, cb)</span> -></span>
(<span class="hljs-property">@events</span>[name]?=[]).push cb
<span class="hljs-attribute">off</span>: <span class="hljs-function"><span class="hljs-params">(name, cb)</span> -></span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@events</span>[name]
<span class="hljs-keyword">if</span> cb <span class="hljs-keyword">then</span> <span class="hljs-property">@events</span>[name] = (l <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> <span class="hljs-property">@events</span>[name] <span class="hljs-keyword">when</span> l <span class="hljs-keyword">isnt</span> cb)
<span class="hljs-keyword">else</span> <span class="hljs-keyword">delete</span> <span class="hljs-property">@events</span>[name]</pre></div></div>
</li>
</ul>
</div>
</body>
</html>