/
index.html
480 lines (405 loc) · 12.4 KB
/
index.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Deferreds</title>
<meta name="description" content="Alex McPherson's 2012 jQuery Conf San Francisco presentation">
<meta name="author" content="Alex McPherson">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link href='http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/print.css" type="text/css" media="print">
<link rel="stylesheet" href="lib/zenburn.css">
</head>
<body>
<div class="reveal">
<!-- Used to fade in a background when a specific slide state is reached -->
<div class="state-background"></div>
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1><span class='code'>.when()</span> to use deferreds</h1>
<h3 class="inverted">Alex McPherson - Quick Left</h3>
</section>
<section>
<h2 class='code'>console.log(talkOutline);</h2>
<ul>
<li class='fragment'>I'm Alex, why you should listen to me</li>
<li class='fragment'>j@ubourg, and a little history</li>
<li class='fragment'>Code perusal</li>
<li class='fragment'>7 problems, 7 solutions</li>
<li class='fragment'>Questions</li>
</ul>
</section>
<section>
<h2 class='code'>alex.define();</h2>
<ul>
<li class='fragment'>Boston → Boulder</li>
<li class='fragment'>Quick Left
<ul>
<li class='fragment'>Front end</li>
<li class='fragment'>Training/Apprenticeship</li>
<li class='fragment'>CU instructor</li>
</ul>
</li>
</ul>
<ul>
<li class='fragment'>Fortune 500 Financial
<ul>
<li class='fragment'>Code Performance</li>
</ul>
</li>
<li class='fragment'>
1-3 month startups
<ul>
<li class='fragment'>"Rapid Prototyping" toolchain</li>
</ul>
</li>
</ul>
</section>
<section>
<h2 class='code'>deferred.history</h2>
<ul>
<li class='fragment'>November 2009, $.ajax functionality requests</li>
<li class='fragment'>AJAX rewritten by Julian Aubourg</li>
<li class='fragment'>Suggested that it be exposed as an API</li>
<li class='fragment'>1/31/2010, 1.5 released</li>
<li class='fragment'>Implementation 'based on' the CommonJS Promises/A proposal</li>
<li class='fragment'>Used all over jQuery, notably in $.ajax and effects</li>
</ul>
</section>
<section>
<h2 class='code'>code.inspect()</h2>
<ul>
<li class='fragment'>
<span>
A Deferred is a state object - 'pending' or 'resolved' stored in a Promise object
</span>
<pre>
<code>
var def = $.Deferred(); //def.state() === 'pending'
<span class='fragment'>
def.resolve(); //def.state() === 'resolved'
</span>
</code>
</pre>
</li>
<li class='fragment'>
<span>
Callback management - Always, Done, Fail
</span>
<pre>
<code>
var def = $.Deferred();
<span class='fragment'>
def.done(function() {
console.log("Deferred resolved!");
});
</span>
<span class='fragment'>
def.resolve(); //logs "Deferred resolved!"
</span>
</code>
</pre>
</li>
</ul>
</section>
<section>
<h2 class='code'>code.inspect()</h2>
<h3 class='fragment'>Callback Management: </h3>
<ul class='fragment'>
<li>deferred.always()</li>
<li>deferred.done()</li>
<li>deferred.fail()</li>
</ul>
<h3 class='fragment'>Change State: </h3>
<ul class='fragment'>
<li>deferred.reject()</li>
<li>deferred.rejectWith(args)</li>
<li>deferred.resolve()</li>
<li>deferred.resolveWith(args)</li>
</ul>
<h3 class='fragment'>Inspect State: </h3>
<ul class='fragment'>
<li>deferred.isRejected()</li>
<li>deferred.isResolved()</li>
<li>deferred.state()</li>
</ul>
<h3 class='fragment'>Chaining Utilities: </h3>
<ul class='fragment'>
<li>deferred.pipe()</li>
<li>deferred.then()</li>
<li>deferred.when()</li>
</ul>
</section>
<section data-state="alert">
<h2 class='code'>alert("Caution");</h2>
<p>This is jQuery Conf, and a <em>lot</em> of this talk is about a jQuery feature, $.Deferred.</p>
<p class='fragment'>That being said, jQuery is a DOM library that mostly facilitates page decoration, events, and AJAX, so this talk will dive deeply into vanilla javascript, and might even mention other frameworks. Keep calm.</p>
</section>
<section data-state="problem">
<section>
<h2>1. Bundling Ajax calls</h2>
<h4>Task at hand:</h4>
<p>Render a small sidebar that needs data from 3 sources.</p>
<h4>First attempt:</h4>
<pre>
<code>
$.ajax({
success: function() {<span class='fragment'>
$.ajax({<span class='fragment'>
//inception-like mess</span>
});</span>
}
});
</code>
</pre>
<!-- $.when vs. other options -->
</section>
<section>
<h2>1. Bundling Ajax calls</h2>
<h4>Task at hand:</h4>
<p>Render a small sidebar that needs data from 3 sources.</p>
<h4>Second attempt:</h4>
<pre>
<code>
var ajaxCallsCompleted = [];<span class='fragment'>
$.ajax({
success: function() {
ajaxCallsCompleted.push('gotAddress');
checkDataCalls();
}
});</span>
<span class='fragment'>
$.ajax({
success: function() {
ajaxCallsCompleted.push('gotTweets');
checkDataCalls();
}
});</span>
<span class='fragment'>
$.ajax({
success: function() {
ajaxCallsCompleted.push('gotFacebook');
checkDataCalls();
}
});
</span>
<span class='fragment'>
var checkDataCalls = function() {
//this checks for all 3 values in the completed array, then calls 'render'
}
</span>
</code>
</pre>
</section>
<section>
<h2>1. Bundling Ajax calls</h2>
<h4>Task at hand:</h4>
<p>Render a small sidebar that needs data from 3 sources.</p>
<h4>Deferreds!</h4>
<pre>
<code>
<span class='fragment'>
var getAddress = function() {
return $.ajax({
//returns a deferred, this is key
});
}
</span>
<span class='fragment'>
var getTweets = function() {
return $.ajax({
});
}
var getFacebook = function() {
return $.ajax({
});
}
</span>
<span class='fragment'>
$.when( getAddress(), getTweets(), getFacebook() ).then(function(){
//render sidebar
})
</span>
</code>
</pre>
</section>
</section>
<section data-state="problem">
<section>
<h2>2. Template Pattern</h2>
<div class='fragment'>
<h4>Task at hand:</h4>
<p>Make a single page app with various pages that load, some of which need data.</p>
</div>
<h4 class='fragment'>Deferreds!</h4>
<pre>
<code><span class='fragment'>
var AboutPage = function(){};
AboutPage.prototype = {
init: function() {
return this.getInfo();
}
}</span>
<span class='fragment'>
var ContactPage = function(){};
ContactPage.prototype = {
init: function() {<span class='fragment'>
return $.Deferred().resolve();
//A deferred not neccessary here, $.when resolved immediately for any type
</span>
}
}
</span>
<span class='fragment'>
var changePage = function(page) {
page.init().done(function() {
page.render();
});
};
</span>
</code>
</pre>
</section>
</section>
<section data-state="problem">
<section>
<h2>3. Publisher/Subscriber</h2>
<!-- Addy link -->
</section>
</section>
<section data-state="problem">
<section>
<h2>4. Cache money</h2>
<!-- Like our QueryEngine -->
</section>
</section>
<section data-state="problem">
<section>
<h2>5. Pretty Syntax and DSLs</h2>
<!-- $.wait, API wrappers, etc -->
</section>
</section>
<section data-state="problem">
<section>
<h2>6. Multiple Callbacks</h2>
<!-- Many success handlers -->
</section>
</section>
<section>
<h2>talk.resolve();</h2>
<p>Quick Left - quickleft.com</p>
<p>@alexmcpherson</p>
<p>McPhersonIndustries.com</p>
</section>
<!-- <section>
<section data-state="alert">
<h2>Global State</h2>
<p>
Set <code>data-state="something"</code> on a slide and <code>"something"</code>
will be added as a class to the document element when the slide is open. This let's you
apply broader style changes, like switching the background.
</p>
<a href="#/7/1" class="image">
<img width="178" height="238" src="https://s3.amazonaws.com/hakim-static/reveal-js/arrow.png">
</a>
</section>
<section data-state="blackout">
<h2>"blackout"</h2>
<a href="#/7/2" class="image">
<img width="178" height="238" src="https://s3.amazonaws.com/hakim-static/reveal-js/arrow.png">
</a>
</section>
<section data-state="soothe">
<h2>"soothe"</h2>
<a href="#/7/0" class="image">
<img width="178" height="238" src="https://s3.amazonaws.com/hakim-static/reveal-js/arrow.png" style="-webkit-transform: rotate(180deg);">
</a>
</section>
</section> -->
<!-- <section data-state="customevent">
<h2>Custom Events</h2>
<p>
Additionally custom events can be triggered on a per slide basis by binding to the <code>data-state</code> name.
</p>
<pre><code contenteditable style="font-size: 18px; margin-top: 20px;">Reveal.addEventListener( 'customevent', function() {
alert( '"customevent" has fired' );
} );
</code></pre>
</section> -->
<!-- <section>
<h2>Pretty Code</h2>
<pre><code contenteditable>
var supports3DTransforms = document.body.style['webkitPerspective'] !== undefined ||
document.body.style['MozPerspective'] !== undefined ||
document.body.style['perspective'] !== undefined;
function linkify( selector ) {
if( supports3DTransforms ) {
var nodes = document.querySelectorAll( selector );
for( var i = 0, len = nodes.length; i < len; i++ ) {
var node = nodes[i];
if( !node.className || !node.className.match( /roll/g ) ) {
node.className += ' roll';
node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>';
}
};
}
}
linkify( 'a' );
</code></pre>
<p>Courtesy of <a href="http://softwaremaniacs.org/soft/highlight/en/description/">highlight.js</a>.</p>
</section> -->
</div>
<!-- The navigational controls UI -->
<aside class="controls">
<a class="left" href="#">◄</a>
<a class="right" href="#">►</a>
<a class="up" href="#">▲</a>
<a class="down" href="#">▼</a>
</aside>
<!-- Displays presentation progress, max value changes via JS to reflect # of slides -->
<div class="progress"><span></span></div>
</div>
<!-- Optional libraries for code syntax highlighting and classList support in IE9 -->
<script src="lib/highlight.js"></script>
<script src="lib/classList.js"></script>
<script src="js/reveal.js"></script>
<script>
// Parse the query string into a key/value object
var query = {};
location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
} );
// Fires when a slide with data-state=customevent is activated
Reveal.addEventListener( 'customevent', function() {
alert( '"customevent" has fired' );
} );
// Fires each time a new slide is activated
Reveal.addEventListener( 'slidechanged', function( event ) {
// event.previousSlide, event.currentSlide, event.indexh, event.indexv
} );
Reveal.initialize({
// Display controls in the bottom right corner
controls: true,
// Display a presentation progress bar
progress: true,
// If true; each slide will be pushed to the browser history
history: true,
// Loops the presentation, defaults to false
loop: false,
// Flags if mouse wheel navigation should be enabled
mouseWheel: true,
// Apply a 3D roll to links on hover
rollingLinks: true,
// UI style
theme: query.theme || 'default', // default/neon
// Transition style
transition: query.transition || 'default' // default/cube/page/concave/linear(2d)
});
hljs.initHighlightingOnLoad();
</script>
</body>
</html>