This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_broker.html
311 lines (203 loc) · 10.7 KB
/
event_broker.html
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE html>
<html>
<head>
<title>event_broker.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 id="jump_to">
<li>
<a class="large" href="javascript:void(0);">Jump To …</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page">
<a class="source" href="chaplin.html">
chaplin.coffee
</a>
<a class="source" href="application.html">
application.coffee
</a>
<a class="source" href="composer.html">
composer.coffee
</a>
<a class="source" href="controller.html">
controller.coffee
</a>
<a class="source" href="dispatcher.html">
dispatcher.coffee
</a>
<a class="source" href="composition.html">
composition.coffee
</a>
<a class="source" href="event_broker.html">
event_broker.coffee
</a>
<a class="source" href="history.html">
history.coffee
</a>
<a class="source" href="route.html">
route.coffee
</a>
<a class="source" href="router.html">
router.coffee
</a>
<a class="source" href="support.html">
support.coffee
</a>
<a class="source" href="sync_machine.html">
sync_machine.coffee
</a>
<a class="source" href="utils.html">
utils.coffee
</a>
<a class="source" href="mediator.html">
mediator.coffee
</a>
<a class="source" href="collection.html">
collection.coffee
</a>
<a class="source" href="model.html">
model.coffee
</a>
<a class="source" href="collection_view.html">
collection_view.coffee
</a>
<a class="source" href="layout.html">
layout.coffee
</a>
<a class="source" href="view.html">
view.coffee
</a>
</div>
</li>
</ul>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>event_broker.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</div>
<div class="content"><div class='highlight'><pre><span class="string">'use strict'</span>
mediator = <span class="built_in">require</span> <span class="string">'chaplin/mediator'</span></pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Add functionality to subscribe and publish to global
Publish/Subscribe events so they can be removed afterwards
when disposing the object.</p>
<p>Mixin this object to add the subscriber capability to any object:
<em>.extend object, EventBroker
Or to a prototype of a class:
</em>.extend @prototype, EventBroker</p>
<p>Since Backbone 0.9.2 this abstraction just serves the purpose
that a handler cannot be registered twice for the same event.</p>
</div>
<div class="content"><div class='highlight'><pre>EventBroker =
<span class="attribute">subscribeEvent</span>: <span class="function"><span class="params">(type, handler)</span> -></span>
<span class="keyword">if</span> <span class="keyword">typeof</span> type <span class="keyword">isnt</span> <span class="string">'string'</span>
<span class="keyword">throw</span> <span class="keyword">new</span> TypeError <span class="string">'EventBroker#subscribeEvent: '</span> +
<span class="string">'type argument must be a string'</span>
<span class="keyword">if</span> <span class="keyword">typeof</span> handler <span class="keyword">isnt</span> <span class="string">'function'</span>
<span class="keyword">throw</span> <span class="keyword">new</span> TypeError <span class="string">'EventBroker#subscribeEvent: '</span> +
<span class="string">'handler argument must be a function'</span></pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Ensure that a handler isn’t registered twice.</p>
</div>
<div class="content"><div class='highlight'><pre> mediator.unsubscribe type, handler, <span class="keyword">this</span></pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Register global handler, force context to the subscriber.</p>
</div>
<div class="content"><div class='highlight'><pre> mediator.subscribe type, handler, <span class="keyword">this</span>
<span class="attribute">unsubscribeEvent</span>: <span class="function"><span class="params">(type, handler)</span> -></span>
<span class="keyword">if</span> <span class="keyword">typeof</span> type <span class="keyword">isnt</span> <span class="string">'string'</span>
<span class="keyword">throw</span> <span class="keyword">new</span> TypeError <span class="string">'EventBroker#unsubscribeEvent: '</span> +
<span class="string">'type argument must be a string'</span>
<span class="keyword">if</span> <span class="keyword">typeof</span> handler <span class="keyword">isnt</span> <span class="string">'function'</span>
<span class="keyword">throw</span> <span class="keyword">new</span> TypeError <span class="string">'EventBroker#unsubscribeEvent: '</span> +
<span class="string">'handler argument must be a function'</span></pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Remove global handler.</p>
</div>
<div class="content"><div class='highlight'><pre> mediator.unsubscribe type, handler</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Unbind all global handlers.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="attribute">unsubscribeAllEvents</span>:<span class="function"> -></span></pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Remove all handlers with a context of this subscriber.</p>
</div>
<div class="content"><div class='highlight'><pre> mediator.unsubscribe <span class="literal">null</span>, <span class="literal">null</span>, <span class="keyword">this</span>
<span class="attribute">publishEvent</span>: <span class="function"><span class="params">(type, args...)</span> -></span>
<span class="keyword">if</span> <span class="keyword">typeof</span> type <span class="keyword">isnt</span> <span class="string">'string'</span>
<span class="keyword">throw</span> <span class="keyword">new</span> TypeError <span class="string">'EventBroker#publishEvent: '</span> +
<span class="string">'type argument must be a string'</span></pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Publish global handler.</p>
</div>
<div class="content"><div class='highlight'><pre> mediator.publish type, args...</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>You’re frozen when your heart’s not open.</p>
</div>
<div class="content"><div class='highlight'><pre>Object.freeze? EventBroker</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Return our creation.</p>
</div>
<div class="content"><div class='highlight'><pre>module.<span class="built_in">exports</span> = EventBroker</pre></div></div>
</li>
</ul>
</div>
</body>
</html>