This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path04.jquery.plugins.html
executable file
·840 lines (741 loc) · 26.9 KB
/
04.jquery.plugins.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Web & Mobile Development — 04.jQuery.Plugins</title>
<meta name="description" content="Web & Mobile Development — 04.jQuery.Plugins">
<meta name="author" content="Bram(us) Van Damme - ikdoeict.be">
<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" media="screen">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- <link rel="stylesheet" href="assets/04/examples.css"> -->
<link rel="stylesheet" href="lib/zenburn.css">
</head>
<body>
<div id="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- 0 : Title -->
<section>
<section>
<h3 class="inverted">Web & Mobile Development <small>JLW288</small></h3>
<h1>04.jQuery.plugins</h1>
<footer>
<em><a href="http://www.ikdoeict.be/">ikdoeict.be</a> — <a href="mailto:[email protected]">[email protected]</a></em>
</footer>
<script>
// Delicously hacky. Look away.
if(navigator.userAgent.match(/(iPhone|iPad|iPod|Android)/i))
document.write('<p style="color: rgba(0,0,0,0.3); text-shadow: none;">('+'Tap to navigate'+')</p>');
</script>
</section>
</section>
<!-- 1 : Intro -->
<section>
<section>
<h2>Plugins</h2>
<p><img src="assets/04/futurama_eyephone.jpg" alt="" title="" height="309" width="446" /></p>
<q>With the new eyePhone, you can watch, listen, ignore your friends, stalk your ex, download porno on a crowded bus, even check your email while getting hit by a train.</q>
</section>
<section>
<h2>Extensibility</h2>
<ul>
<li class="fragment">
jQuery is built in such a way that it is very extensible
</li>
<li class="fragment">
Extensions are released as plugins
<ul>
<li class="fragment">
(Cluttered) Overview available at <a href="http://plugins.jquery.com/">http://plugins.jquery.com/</a>
<ul>
<li class="fragment">Most of the time you're better off with <a href="http://www.flickr.com/photos/bramus/2998573943/">A Google Search Coupon</a></li>
</ul>
</li>
<li class="fragment">Some bigger extensions are released as projects
<ul>
<li class="fragment"><a href="http://jqueryui.com/">jQuery UI</a> — abstractions for low-level interaction and animation, advanced effects and high-level, themeable widgets</li>
<li class="fragment"><a href="http://jquerymobile.com/">jQuery Mobile</a> — Touch-Optimized Web Framework for Smartphones & Tablets</li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Using Plugins</h2>
<ul>
<li class="fragment">
Include it in your HTML page after you've included jQuery and the new functionality becomes available
<pre class="bigger"><code class="language-html dontrun" data-overlay-example="assets/04/examples/01.html"><!doctype html>
<html lang="en">
<head>
<title>jQuery Plugins - 01</title>
</head>
<body>
<h1>jQuery.reverseText</h1>
<p>Hi, I will be reversed!</p>
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery.reversetext.js"></script>
<script>
$(document).ready(function() {
$('p').reverseText();
});
</script>
</body>
</html></code></pre>
</li>
</ul>
</section>
<section>
<h2>Plugin Types</h2>
<ul>
<li class="fragment">
Plugins can extend the core/utility functions
<ul>
<li class="fragment">Not performed on elements</li>
<li class="fragment">viz. helper functions like <code>$.trim()</code></li>
<li class="fragment">viz. a function <code>$.randomColor()</code> to get a random color returned.</li>
</ul>
</li>
<li class="fragment">
Plugins can extend the supported selectors/expressions
<ul>
<li class="fragment">viz. <code>$(':lists')</code> to select all <code><ul></code>, <code><ol></code> and <code><dl></code> elements</li>
<li class="fragment">viz. <code>$(':inview')</code> to select all elements inside the current viewport</li>
<li class="fragment">viz. <code>$(':external')</code> to select all links that link to external websites</li>
</ul>
</li>
<li class="fragment">
Plugins can extend jQuery instance functions
<ul>
<li class="fragment">viz. roll out your own effects such as <code>$(els).slideAndRemove();</code></li>
<li class="fragment">viz. reverse the text of an element <code>$(els).reverseText();</code></li>
<li class="fragment">viz. inject descriptions next to images based on their <code>alt</code> attribute with <code>$('img[alt]').addDescription();</code></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Extensibility Side note</h2>
<ul>
<li class="fragment">
The jQuery you download actually is a jQuery Object bundled with some common plugins
<ul>
<li class="fragment">
Some of the jQuery functionality originally were external plugins <em>(e.g. <code>Ajax</code> & <code>Dimensions</code>)</em> and were later on moved into jQuery itself
</li>
<li class="fragment">
<a href="http://www.keyframesandcode.com/resources/javascript/deconstructed/jquery/">jQuery Deconstructed</a> gives a nice view on how jQuery is organized
</li>
</ul>
</li>
</ul>
</section>
</section>
<!-- 2 : Writing Plugins -->
<section>
<section>
<h2>Writing Plugins</h2>
<p><img src="assets/04/futurama_benderpointing.jpg" alt="" title="" height="309" width="446" /></p>
<q>Yeah, well... I'm gonna go build my own theme park, with blackjack and hookers. In fact, forget the park!</q>
</section>
<section>
<h2>Stuff you should know about jQuery</h2>
<ul>
<li class="fragment">
The core object of jQuery is named <code>jQuery</code>
<ul>
<li>
It is mapped to <code>$</code> for convenience
<pre class="bigger"><code class="language-javascript dontrun">// These two have the same result
var p1 = $('p'),
p2 = jQuery('p');</code></pre>
</li>
</ul>
</li>
<li class="fragment">
jQuery Object functions like <code>.slideUp()</code> are defined in <code>jQuery.prototype</code>
<ul>
<li class="fragment">
<code>jQuery.prototype</code> is mapped to <code>jQuery.fn</code> (or <code>$.fn</code>) for convenience
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Writing Plugins</h2>
<ul>
<li class="fragment">
Three types of plugins ...
<ol>
<li>Plugins can extend the core/utility functions</li>
<li>Plugins can extend the supported selectors/expressions</li>
<li>Plugins can extend jQuery instance functions</li>
</ol>
</li>
<li class="fragment">
... equals three ways to writing plugins
<ol>
<li>Extending the jQuery Object (<code>$</code>)</li>
<li>Extending <code>$.expr</code></li>
<li>Extending jQuery's <code>.prototype</code> (<code>$.fn</code>)</li>
</ol>
</li>
</ul>
</section>
</section>
<!-- 3 : Writing Plugins : Utility Functions -->
<section>
<section>
<h2>Writing Utility Functions</h2>
<p><img src="assets/04/futurama_basic.jpg" alt="" title="" height="309" width="446" /></p>
<q>Do refrigerators still come in cardboard boxes?</q><br /><q>Yeah, but the rents are outrageous.</q>
</section>
<section>
<h2>Writing Utility Functions</h2>
<ul>
<li class="fragment">
Use jQuery's built-in <a href="http://api.jquery.com/jQuery.extend/">.extend()</a> to extend itself
<blockquote style="margin-top: 20px;">If only one argument is supplied to <code>$.extend()</code>, this means the target argument was omitted. In this case, the jQuery object itself is assumed to be the target. By doing this, we can add new functions to the jQuery namespace. This can be useful for plugin authors wishing to add new methods to jQuery.</blockquote>
</li>
</ul>
</section>
<section>
<h2>Example</h2>
<ul>
<li class="fragment">
Method to get a random color
<pre class="bigger"><code class="language-javascript dontrun">var randomColor = function() {
return '#' + Math.floor(Math.random() * 16777216).toString(16);
}</code></pre>
</li>
<li class="fragment">
jQuery version:
<pre class="bigger"><code class="language-javascript dontrun">$.extend({
randomColor : function() {
return '#' + Math.floor(Math.random() * 16777216).toString(16);
}
})</code></pre>
</li>
<li class="fragment">
As you're passing an object into <code>$.extend</code>, it's possible to pass in/define multiple functions at once
</li>
</ul>
</section>
<section>
<h2>Usage</h2>
<ul>
<li class="fragment">
The name of the function has become available on <code>$</code>
<pre class="bigger"><code class="language-html dontrun" data-overlay-example="assets/04/examples/02.html"><!doctype html>
<html lang="en">
<head>
<title>jQuery Plugins - 02</title>
</head>
<body>
<h1>jQuery.randomColor</h1>
<p>Click me to change backgroundColor</p>
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery.randomcolor.js"></script>
<script>
$(document).ready(function() {
$('p').css('cursor','pointer').on('click', function(e) {
$(this).css('background', $.randomColor());
});
});
</script>
</body>
</html></code></pre>
</li>
</ul>
</section>
<section>
<h2>Code Improvement</h2>
<ul>
<li class="fragment">
Use an IIFE to make sure <code>$</code> refers to <code>jQuery</code>
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.extend({
randomColor : function() {
return '#' + Math.floor(Math.random() * 16777216).toString(16);
}
})
})(jQuery);</code></pre>
<ul>
<li>Always use the IIFE</li>
</ul>
</li>
</ul>
</section>
</section>
<!-- 4 : Writing Plugins : Writing Custom Expressions -->
<section>
<section>
<h2>Writing Custom Expressions</h2>
<p><img src="assets/04/futurama_sb.jpg" alt="" title="" height="309" width="446" /></p>
<q>Yes, I'd like to make a collect call.</q>
</section>
<section>
<h2>Writing Custom Expressions</h2>
<ul>
<li class="fragment">
Custom expressions are stored in <code>jQuery.expr[':']</code>
</li>
<li class="fragment">
Use <code>jQuery.extend()</code> to extend <code>jQuery.expr[':']</code> with a new function
<pre class="bigger"><code class="language-javascript dontrun">jQuery.extend(jQuery.expr[':'], {
example : function(elem, index, meta, stack) {
// elem - is a current DOM element
// index - the current loop index in stack
// meta - meta data about your selector
// stack - stack of all elements to loop
// Return true to include current element
// Return false to explude current element
}
});</code></pre>
<ul>
<li class="fragment">
Most of the time you'll only need the <code>elem</code> parameter
<ul>
<li class="fragment"><code>elem</code> is not a jQuery Instance!</li>
</ul>
</li>
<li class="fragment">The <code>meta</code> param is an array with info about your selector, <a href="http://jquery-howto.blogspot.com/2009/06/jquery-custom-selectors-with-parameters.html">allows you to use parameters</a> with your selector</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Example</h2>
<ul>
<li class="fragment">
Select all possible lists
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.extend($.expr[':'], {
lists : function(elem, index, match, stack) {
var els = ['ul','ol','dl'];
return ($.inArray(elem.nodeName.toLowerCase(), els) > -1);
}
});
})(jQuery);</code></pre>
</li>
<li class="fragment">
Usage
<pre class="bigger"><code class="language-javascript dontrun" data-overlay-example="assets/04/examples/03.html">$(':lists').css('border', '1px solid red');</code></pre>
</li>
<li class="fragment">
Notes
<ul>
<li class="fragment">
These selectors perform badly (slow), as they loop all elements
<ul>
<li>Best to only use to refine a selection: <code>$('#somediv').find(':lists');</code></li>
</ul>
</li>
<li class="fragment">This performs better (and does the same): <code>$('ul,ol,dl');</code></li>
</ul>
</li>
</ul>
</section>
</section>
<!-- 4 : Writing Plugins : Writing jQuery Instance Functions -->
<section>
<section>
<h2>Writing Instance Functions</h2>
<p><img src="assets/04/futurama_brains.jpg" alt="" title="" height="309" width="446" /></p>
<q>Somehow he has cobbled together a random assortment of other brain waves into a working mind.</q>
</section>
<section>
<h2>Writing Instance Functions</h2>
<ul>
<li class="fragment">
Extend <code>jQuery.prototype</code>/<code>$.fn</code> as you'd extend a regular <code>.prototype</code>
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.fn.myPlugin = function() {
// ... plugin code here
}
})(jQuery);</code></pre>
</li>
<li class="fragment">
Also possible
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.fn.extend({
myPlugin : function() {
// ... plugin code here
}
});
})(jQuery);</code></pre>
</li>
<li class="fragment">
Usage
<pre class="bigger"><code class="language-javascript dontrun">$('...').myPlugin();</code></pre>
</li>
</ul>
</section>
<section>
<h2>What is <code>this</code>?</h2>
<ul>
<li class="fragment">
The <code>this</code> inside your plugin is a jQuery instance (in contrast to other situations where it's a DOM element)
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.fn.slideUpAndRemove = function() {
this.slideUp("slow", function() { // <-- jQuery Instance
$(this).remove();
});
}
})(jQuery);</code></pre>
</li>
</ul>
</section>
<section>
<h2>Chainability</h2>
<ul>
<li class="fragment">
To not break chainability one <strong>must</strong> return the instance
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.fn.slideUpAndRemove = function() {
return this.slideUp("slow", function() {
$(this).remove();
});
}
})(jQuery);</code></pre>
</li>
<li class="fragment">
Mostly you'll loop all selected elements and still must return the instance
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.fn.myPlugin = function() {
return this.each(function() {
// ... plugin code here
});
}
})(jQuery);</code></pre>
</li>
<li class="fragment">
In short: always return <code>this</code>
</li>
</ul>
</section>
<section>
<h2>Example</h2>
<ul>
<li class="fragment">
Inject the link locations after each link
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.fn.showLinkLocation = function() {
return this.filter('a').each(function() {
$(this).append( ' <em>(' + this.href + ')</em>');
});
};
}(jQuery));</code></pre>
</li>
<li class="fragment">
Usage
<pre class="bigger"><code class="language-javascript dontrun" data-overlay-example="assets/04/examples/04.html">$('a').showLinkLocation().css('text-decoration','none');</code></pre>
</li>
</ul>
</section>
<section>
<h2>Working with parameters (1)</h2>
<ul>
<li class="fragment">
Possible, best to work with an object and with defaults
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.fn.hilight = function(options) {
var defaults = {
foreground: 'red',
background: 'yellow'
};
// Extend our default options with those provided.
var o = $.extend({}, defaults, options);
return this.each(function() {
// ... plugin code here
});
};
}(jQuery));</code></pre>
</li>
</ul>
</section>
<section>
<h2>Working with parameters (2)</h2>
<ul>
<li class="fragment">
Best to put <code>defaults</code> outside the plugin definition, so they can be adjusted by the user
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.fn.hilight = function(options) {
// Extend our default options with those provided.
var o = $.extend({}, $.fn.hilight.defaults, options);
return this.each(function() {
// ... plugin code here
});
};
$.fn.hilight.defaults = {
foreground: 'red',
background: 'yellow'
};
}(jQuery));</code></pre>
</li>
</ul>
</section>
<section>
<h2>Putting it together</h2>
<ul>
<li class="fragment">
Plugin code
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.fn.hilight = function(options) {
var o = $.extend({}, $.fn.hilight.defaults, options);
return this.each(function() {
var $this = $(this);
$this.css({
'color': o.foreground,
'background-color': o.background
});
$this.html('<strong>' + $this.html() + '</strong>');
});
};
$.fn.hilight.defaults = {
foreground: 'red',
background: 'yellow'
};
}(jQuery));</code></pre>
</li>
<li class="fragment">
Usage
<pre class="bigger"><code class="language-javascript dontrun" data-overlay-example="assets/04/examples/05.html">$('em').hilight();
$('a').hilight({foreground: '#00F'});</code></pre>
</li>
</ul>
</section>
</section>
<!-- 2 : Writing Plugins : Advanced jQuery Instance Functions -->
<section>
<section>
<h2>Advanced Instance Functions</h2>
<p><img src="assets/04/futurama_infosphere.jpg" alt="" title="" height="309" width="446" /></p>
<q>This is it. The moment we should've trained for.</q>
</section>
<section>
<h2>Advanced Instance Functions (1)</h2>
<ul>
<li class="fragment">
When allowing one to remove hilight, we could do this
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
$.fn.hilight = function(options) {
// ... plugin code here
};
$.fn.hilightRemove = function() {
// ... plugin code here
};
$.fn.hilight.defaults = {
foreground: 'red',
background: 'yellow'
};
}(jQuery));</code></pre>
<ul>
<li class="fragment">
This is bad idea, things will become cluttered when offering lots of functionality
</li>
<li class="fragment">
We've had this problem before, and then solved it by namespacing our code
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Advanced Instance Functions (2)</h2>
<ul>
<li class="fragment">
What if we could do this?
<pre class="bigger"><code class="language-javascript dontrun">$('em').hilight();
$('em').on('click', function() {
$(this).hilight('remove');
});</code></pre>
<ul>
<li class="fragment">
Code much cleaner
</li>
<li class="fragment">
No function loitering
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>A Design Pattern</h2>
<ul>
<li class="fragment">
jQuery has a design pattern for this
<pre class="bigger"><code class="language-javascript dontrun">(function($) {
var methods = {
init : function(options) {
// ... plugin code here
},
otherfunction : function() {
// ... plugin code here
}
};
$.fn.pluginName = function(method) {
if (methods[method]) {
return methods[method].apply(
this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.plugin');
}
};
})(jQuery);</code></pre>
<ul>
<li class="fragment">Call plugin without a function name and <code>init</code> will be called.</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Putting it together (1)</h2>
<div class="fragment"><pre class="bigger"><code class="language-javascript dontrun">(function($) {
var methods = {
init : function(options) {
var o = $.extend({}, $.fn.hilight.defaults, options);
return this.each(function() {
var $this = $(this);
$this.css({
'color': o.foreground,
'background-color': o.background
});
$this.html('<strong>' + $this.html() + '</strong>');
});
},
remove : function() {
return this.each(function(){
var $this = $(this);
$this.css({
'color': 'inherit',
'background-color': 'transparent'
});
$this.html($this.find('strong').html());
});
}
};
// ...</code></pre>
</div>
</section>
<section>
<h2>Putting it together (2)</h2>
<div class="fragment"><pre class="bigger"><code class="language-javascript dontrun">// ...
$.fn.hilight = function(method) {
if (methods[method]) {
return methods[method].apply(
this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.hilight');
}
};
$.fn.hilight.defaults = {
foreground: 'red',
background: 'yellow'
};
})(jQuery);</code></pre>
</div>
</section>
<section>
<h2>Putting it together (3)</h2>
<div>
Usage
<pre class="bigger"><code class="language-javascript dontrun" data-overlay-example="assets/04/examples/06.html">// Add highlights
$('em').hilight();
$('a').hilight({foreground: '#00F'});
// remove highlight after 1 second
setTimeout(function() {
$('em').hilight('remove').css('border', '1px solid red');
}, 1000);</code></pre>
</div>
</section>
<section>
<h2>One more thing</h2>
<ul>
<li class="fragment">
jQuery plugins are stateless
<ul>
<li class="fragment">e.g. No way to know the original color when calling <code>$('...').hilight('remove')</code></li>
<li class="fragment">Some shortcomings can be circumvented by storing data on the elements via <code>$.data</code></li>
</ul>
</li>
<li class="fragment">
Stateful plugins possible with jQuery UI's Widget Factory
<ul>
<li class="fragment">A more robust/advanced way to creating plugins</li>
<li class="fragment">
References <em>(outside the scope of this course)</em>
<ul>
<li><a href="http://stage.learn.jquery.com/plugins/stateful-plugins-with-widget-factory/">Writing Stateful Plugins with the jQuery UI Widget Factory</a></li>
<li><a href="http://wiki.jqueryui.com/w/page/12138135/Widget%20factory">Widget Factory</a></li>
<li><a href="http://ajpiano.com/widgetfactory/">The jQuery UI Widget Factory: Wat? <em>(Slides)</em></a></li>
<li><a href="http://www.erichynds.com/jquery/tips-for-developing-jquery-ui-widgets/">Tips for Developing jQuery UI 1.8 Widgets</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
</section>
<!-- The END -->
<section>
<section>
<h2>Questions?</h2>
<footer>
<em><a href="http://www.ikdoeict.be/">ikdoeict.be</a> — <a href="mailto:[email protected]">[email protected]</a></em>
</footer>
</section>
</section>
<!-- Sources -->
<section id="sources">
<section>
<h2>Sources</h2>
<ul>
<li><a href="http://www.keyframesandcode.com/resources/javascript/deconstructed/jquery/">jQuery Deconstructed</a></li>
<li><a href="http://docs.jquery.com/Plugins/Authoring">jQuery Docs: Plugins Authoring</a></li>
<li><a href="http://stage.learn.jquery.com/plugins/a_plugin_development_pattern/">learn.jquery.com: A Plugin Development Pattern</a></li>
<li><a href="http://stage.learn.jquery.com/plugins/basic-plugin-creation/">learn.jquery.com: Basic Plugin Creation</a></li>
<li><a href="http://stage.learn.jquery.com/plugins/stateful-plugins-with-widget-factory/">learn.jQuery.com: Stateful Plugins with Widget Factory</a></li>
<li><a href="http://asimilia.wordpress.com/2008/12/17/jquery-extend-confusion/"><code>$.extend</code> vs. <code>$.fn.extend</code></a></li>
<li><a href="http://ejohn.org/apps/workshop/intro/#35">Custom Effect Example</a></li>
<li><a href="http://ejohn.org/apps/workshop/intro/#36">Custom Expression Example</a></li>
<li><a href="http://www.sitepoint.com/how-to-develop-a-jquery-plugin/">Reverse Text Plugin</a></li>
</ul>
</section>
</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>
<span id="revealIndex">/</span>
</aside>
<!-- Index Link -->
<aside class="back">
<a href="index.html">← Back to index</a>
</aside>
<!-- ikdoeict.be Link -->
<a href="http://www.ikdoeict.be/" title="ikdoeict.be" id="ikdoeict">ikdoeict.be</a>
<!-- Displays presentation progress, max value changes via JS to reflect # of slides -->
<div class="progress"><span></span></div>
</div>
<script src="js/reveal.js"></script>
<script src="lib/highlight.js"></script>
<script src="lib/prefixfree.js"></script>
<script src="lib/css-snippets.js"></script>
<script src="lib/css-edit.js"></script>
<script src="lib/incrementable.js"></script>
<script src="js/main.js"></script>
</body>
</html>