-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1995 lines (1453 loc) · 44.1 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
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- 2017-11-23 Thu 19:35 -->
<meta charset="utf-8" />
<meta htto-equiv="X-UA-Compatible" content="chrome=1" />
<meta name="generator" content="Org-mode with org-ioslide" />
<meta name="author" content="fatfingererr @ ClojureTW 十一月聚會" />
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">-->
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0">-->
<!--This one seems to work all the time, but really small on ipad-->
<!--<meta name="viewport" content="initial-scale=0.4">-->
<meta name="apple-mobile-web-app-capable" content="yes" />
<link rel="stylesheet" media="all" href="theme/css/default.css" />
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="theme/css/phone.css" />
<link rel="stylesheet" media="all" href="theme/css/small-icon.css" />
<base target="_blank"> <!-- This amazingness opens all links in a new tab. -->
<script data-main="js/slides" src="js/require-1.0.8.min.js"></script>
<script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="js/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML,local/local" type="text/javascript"></script>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<slide class="title-slide segue nobackground">
<aside class="gdbar"><img src="images/clojurescript.png"></aside>
<!-- The content of this hgroup is replaced programmatically through the slide_config.json. -->
<hgroup class="auto-fadein">
<h1 data-config-title><!-- populated from slide_config.json --></h1>
<h2 data-config-subtitle><!-- populated from slide_config.json --></h2>
<p data-config-presenter><!-- populated from slide_config.json --></p>
</hgroup>
</slide>
<slide id="sec-" class=" segue dark quote nobackground" style="background-image: url(nil)">
<aside class="gdbar right bottom"><img src="images/clojurescript.png"></aside><hgroup class="">
<h2 class=" "><div id="outline-container-orgfbc8b81" class="outline-2">
<h2 id="orgfbc8b81">A Deeper Understanding of Domina Events</h2>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="flexbox vleft auto-fadein" id="text-">
<p>
</p>
</article>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-orgb2b7655" class="outline-3">
<h3 id="orgb2b7655">漸進增強策略 (Progressive Enhancement)</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>在第四章中我們提到一種網頁設計的策略,就是漸進增強</li>
</ul>
<ul>
<li>這種策略能避免瀏覽器的兼容性問題。</li>
</ul>
<ul>
<li>在漸進增強策略中,網頁應該提供兩種瀏覽方案:</li>
</ul>
<ul>
<li>1. 不支援 JS (或被停用)的基本使用者體驗</li>
</ul>
<ul>
<li>2. 支援 JS 與 Ajax 的完整使用者體驗</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-orgcc99d9f" class="outline-3">
<h3 id="orgcc99d9f">漸進增強策略 (Progressive Enhancement)</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>一般來說,我們應該首先支持基本使用者體驗</li>
</ul>
<ul>
<li>接著使網頁能支援 JS</li>
</ul>
<ul>
<li>最終引入 Ajax 達到提供完整使用者體驗的目的</li>
</ul>
<ul>
<li>接著我們會用 Tutorial 4 的 Login Form 舉例說明</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-orgcc39579" class="outline-3">
<h3 id="orgcc39579">使用漸進增強策略改善 Login Form</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>首先先來看 index.html :</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="html">
...
<div>
<label for="submit"></label>
<input type="submit" value="Login &rarr;" id="submit">
</div>
...</pre>
</div>
<ul>
<li>我們可以看到 input 的 type 是 submit</li>
</ul>
<ul>
<li>前面學過我們可以把 type 換成 button 但會要求 JS 的實現</li>
</ul>
<ul>
<li>會不符合漸進增強的策略</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org8deecf4" class="outline-3">
<h3 id="org8deecf4">使用 domina 的 listen!</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>這邊我們可以使用 domina 的 <code>listen!</code> 函數</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
;;; namespace declaration
(ns modern-cljs.login
(:require <b>[domina.core :refer [by-id value]]
[domina.events :refer [listen!]]</b>))</pre>
</div>
<ul>
<li>來根據我們在 submit 上面做了點擊後,執行帳號密碼的驗證</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
;;; init
(defn ^:export init []
(if (and js/document
(aget js/document "getElementById"))
<b>(listen! (by-id "submit") :click validate-form)</b>))</pre>
</div>
</article>
</slide>
</slide>
<slide id="sec-" class="fill white nobackground" style="background-image: url(images/listen-1.png)">
<hgroup class="">
<h2 class="white "><div id="outline-container-orgf3845d8" class="outline-3">
<h3 id="orgf3845d8"></h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org83577dd" class="outline-3">
<h3 id="org83577dd">使用 domina 的 listen!</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>雖然我們可以等待 sumbit 被 click 之後進行驗證</li>
</ul>
<ul>
<li>但是表單依舊會執行我們定義的行為,提交帳號密碼到伺服器端</li>
</ul>
<ul>
<li>但是帳號密碼如果是驗證無效的話,我們應該要擋下這個行為</li>
</ul>
<ul>
<li>因此我們要使用 <code>domina.events</code> 中的 <code>prevent-default</code> 幫忙處理這件事</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" class="fill white nobackground" style="background-image: url(images/prevent-default.png)">
<hgroup class="">
<h2 class="white "><div id="outline-container-org5b71253" class="outline-3">
<h3 id="org5b71253"></h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-orgbd75f27" class="outline-3">
<h3 id="orgbd75f27">使用 domina 的 listen!</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>先引入 <code>domina.events</code> :</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
(ns modern-cljs.login
(:require [domina.core :refer [by-id value]]
<b>[domina.events :refer [listen! prevent-default]]</b>))</pre>
</div>
<ul>
<li>就是說在 <code>validate-form</code> 擋下提交帳號密碼到伺服器端:</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
(defn validate-form [e]
(if (or (empty? (value (by-id "email")))
(empty? (value (by-id "password"))))
(do
<b>(prevent-default e)</b>
(js/alert "Please, complete the form!"))
true))</pre>
</div>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org53dad2d" class="outline-3">
<h3 id="org53dad2d">使用 domina 的 listen!</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>如果仔細留意的話, <code>validate-form</code> 不會像原本的一樣回傳 <code>false</code></li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
(defn validate-form [e]
(if (or (empty? (value (by-id "email")))
(empty? (value (by-id "password"))))
<b>(do
(prevent-default e)
(js/alert "Please, complete the form!"))</b> ;; return false?
true))</pre>
</div>
<ul>
<li>因為我們這邊已經用 <code>prevent-default</code> 擋下表單提交</li>
</ul>
<ul>
<li>因此就不需要回傳 <code>false</code> 來做進一步處理</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org641615e" class="outline-3">
<h3 id="org641615e">使用 domina 的 listen!</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>但是 <code>validate-form</code> 需要輸入參數,也就是事件 <code>e</code> 才能擋下提交:</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
<b>(defn validate-form [e]</b>
...
<b>(prevent-default e)</b>
(js/alert "Please, complete the form!"))
)</pre>
</div>
<ul>
<li>所以 <code>init</code> 函數需要做一個匿名函數放入事件參數</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
(defn ^:export init []
(if (and js/document
(aget js/document "getElementById"))
(listen! (by-id "submit") :click <b>(fn [e] (validate-form e))</b>)))</pre>
</div>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org0c05c35" class="outline-3">
<h3 id="org0c05c35">輸入當下立即驗證</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>有一些網站在你輸入帳號密碼結束時,就能直接幫你做驗證</li>
</ul>
<ul>
<li>這樣能幫助使用者得到立即反饋,更好的輸入帳號和密碼</li>
</ul>
<ul>
<li>我們現在也可以來做一個,使用正則表達式驗證(regex validators)</li>
</ul>
<ul>
<li>分別來驗證 email 和 password</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" class="fill white nobackground" style="background-image: url(images/validate-email.png)">
<hgroup class="">
<h2 class="white "><div id="outline-container-orgd8df6ed" class="outline-3">
<h3 id="orgd8df6ed"></h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-orgb7e204d" class="outline-3">
<h3 id="orgb7e204d">輸入當下立即驗證</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>我們可以建立兩個帶有 <code>:dynamic</code> 的正則驗證的變數</li>
</ul>
<ul>
<li>當使用 <code>:dynamic</code> 時,我們就不需要傳遞正則表達式到底層的驗證函數</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
;;; 4 to 8, at least one numeric digit.
(def ^:dynamic *password-re*
#"^(?=.*\d).{4,8}$")
(def ^:dynamic *email-re*
#"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$")</pre>
</div>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org760c212" class="outline-3">
<h3 id="org760c212">輸入當下立即驗證</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>接著我們新增一個監視用戶跳離 email 與 password 輸入欄位的功能</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
(defn ^:export init []
(if (and js/document
(aget js/document "getElementById"))
(let [email (by-id "email")
password (by-id "password")]
...
<b>(listen! email :blur (fn [evt] (validate-email email)))
(listen! password :blur (fn [evt] (validate-password password)))</b>)))</pre>
</div>
<ul>
<li>其中 <code>validate-email</code> 和 <code>validate-password</code> 就是我接著要實現的正則表達式的驗證函數</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-orgb34f94d" class="outline-3">
<h3 id="orgb34f94d">輸入當下立即驗證</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>接著再來建立我們的即時驗證的函數</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
(defn validate-email [email]
(destroy! (by-class "email"))
(if (not (re-matches *email-re* (value email)))
(do (prepend! (by-id "loginForm") (html [:div.help.email "Wrong email"]))
false) true))
...
(defn validate-password [password]
(destroy! (by-class "password"))
(if (not (re-matches *password-re* (value password)))
(do (append! (by-id "loginForm") (html [:div.help.password "Wrong password"]))
false) true))</pre>
</div>
</article>
</slide>
</slide>
</slide>
<slide id="sec-" class=" segue dark quote nobackground" style="background-image: url(nil)">
<aside class="gdbar right bottom"><img src="images/clojurescript.png"></aside><hgroup class="">
<h2 class=" "><div id="outline-container-org6ca0642" class="outline-2">
<h2 id="org6ca0642">HTML on Top, Clojure on the Bottom</h2>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="flexbox vleft auto-fadein" id="text-">
<p>
</p>
</article>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org028f250" class="outline-3">
<h3 id="org028f250">HTML5 的新功能</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>這邊會簡單介紹一些 HTML5 的新功能,我們先看 <code>index.html</code></li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="html">
<form action="login.php" method="post" id="loginForm">
...
<input type="email" name="email" id="email"
placeholder="email"
title="Type a well-formed email!"
pattern="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"
required>
...</pre>
</div>
<ul>
<li>你可以發現 <code>pattern</code> 在做的事情就是我們前面有做過的驗證</li>
</ul>
<ul>
<li>我們可以把它刪掉,用 <code>domina</code> 的做法完成這件事(後略)</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-orge3c0ea0" class="outline-3">
<h3 id="orge3c0ea0">伺服器端的漸進增強改善</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>我們一直沒有處理伺服器端如何應對表單提交</li>
</ul>
<ul>
<li>其中在 <code>core.clj</code> 應該對 routes 有定義,來處理表單提交的 POST</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojure">
(ns modern-cljs.core
(:require <b>[compojure.core :refer [defroutes GET POST]] ; <- add POST</b>
[compojure.route :refer [not-found files resources]]))
...
(defroutes handler
(GET "/" [] "Hello from Compojure!") ;; for testing only
(files "/" {:root "target"}) ;; to serve static resources
<b>(POST "/login" [email password] (authenticate-user email password)) ; <- add POST route</b>
(resources "/" {:root "target"}) ;; to serve anything else
(not-found "Page Not Found")) ;; page not found</pre>
</div>
</article>
</slide>
</slide>
<slide id="sec-" class="fill white nobackground" style="background-image: url(images/authenticate-user.png)">
<hgroup class="">
<h2 class="white "><div id="outline-container-orgbd1f1bb" class="outline-3">
<h3 id="orgbd1f1bb"></h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-orgff35480" class="outline-3">
<h3 id="orgff35480">伺服器端的漸進增強改善</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>同時我們也要再開一個新檔案 <code>login.clj</code> 做和 <code>login.cljs</code> 一樣的事</li>
</ul>
<ul>
<li>讓我們在伺服器端也能驗證帳號密碼,底下只舉例 <code>authenticate-user</code> 函數:</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
(defn authenticate-user [email password]
(if (or (empty? email) (empty? password))
(str "Please complete the form")
(if (and (validate-email email)
(validate-password password))
(str email " and " password
" passed the formal validation, but you still have to be authenticated"))))</pre>
</div>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org6184f4a" class="outline-3">
<h3 id="org6184f4a">伺服器端的漸進增強改善</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>並且把函數新增到 <code>core.clj</code> 在伺服器端使用</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
(ns modern-cljs.core
(:require [compojure.core :refer [defroutes GET POST]]
[compojure.route :refer [not-found files resources]]
<b>[modern-cljs.login :refer [authenticate-user]]</b>))
...</pre>
</div>
<ul>
<li>你會很明顯發現 <code>login.clj</code> 的程式碼和 <code>login.cljs</code> 重複</li>
</ul>
<ul>
<li>暫且先如此,後面會解決這個問題</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" class="fill white nobackground" style="background-image: url(images/clj-and-cljs.png)">
<hgroup class="">
<h2 class="white "><div id="outline-container-orgb6ef962" class="outline-3">
<h3 id="orgb6ef962"></h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
</article>
</slide>
</slide>
</slide>
<slide id="sec-" class=" segue dark quote nobackground" style="background-image: url(nil)">
<aside class="gdbar right bottom"><img src="images/clojurescript.png"></aside><hgroup class="">
<h2 class=" "><div id="outline-container-org7bc28a4" class="outline-2">
<h2 id="org7bc28a4">Don't Repeat Yourself</h2>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="flexbox vleft auto-fadein" id="text-">
<p>
Tutorial 12
</p>
</article>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org6b9c57d" class="outline-3">
<h3 id="org6b9c57d">DRY 原則</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>前一章我們討論到使用漸進增強策略進行開發</li>
</ul>
<ul>
<li>最後我們在客戶端和伺服器端都有重複的驗證函數,這樣不行</li>
</ul>
<ul>
<li>進一步我們要討論如何遵守 <a href="https://zh.wikipedia.org/zh-tw/%E4%B8%80%E6%AC%A1%E4%B8%94%E4%BB%85%E4%B8%80%E6%AC%A1">DRY(Don't Repeat Yourself)原則</a></li>
</ul>
<article class="flexbox vcenter">
<div class="figure">
<p><img src="images/dry-apply.png" alt="dry-apply.png" width="500px" />
</p>
</div>
</article>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org76380f7" class="outline-3">
<h3 id="org76380f7">DRY 原則:驗證問題</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>如果把 DRY 原則應用到現在程式碼重複的驗證流程,代表:</li>
</ul>
<ul>
<li>1. 首先得選擇一個驗證函式庫,能在伺服器端與客戶端驗證資料</li>
</ul>
<ul>
<li>2. 接著定義在伺服器端與客戶端都能驗證的驗證集</li>
</ul>
<ul>
<li>3. 最終,我們測試我們的驗證是正確的</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org0297d2c" class="outline-3">
<h3 id="org0297d2c">選擇驗證函式庫(Validator library)</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>如果你找 Clojure 的驗證函式庫會找到很多</li>
</ul>
<ul>
<li>但是如果在 2012 年找尋 ClojureScript 的驗證函式庫,則只會找到 <a href="https://github.com/cemerick/valip">Valip</a></li>
</ul>
<blockquote>
<p>
由於 Clojure 的蓬勃發展,現在(2017)已經有許多 cljs 的驗證函式庫
</p>
</blockquote>
<ul>
<li>Valip 是 <a href="https://github.com/weavejester/valip">原本的 Valip</a> 的 Fork ,但是也可以在 ClojueScript 中使用</li>
</ul>
<ul>
<li>在本 Tutorial 中 Valip 就足夠使用,後續就以此函式庫做講解</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org7f9dbbd" class="outline-3">
<h3 id="org7f9dbbd">Valip 函數庫:validate</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<ul>
<li>讓我們開始先研究 <code>Valip</code> 函式庫有什麼功能</li>
</ul>
<ul>
<li>先認識函數 <code>validate</code> :</li>
</ul>
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
(validate {:key-1 hvalue-1 :key-2 value-2 ... :key-n value-n}
[key-1 predicate-1 error-1]
[key-2 predicate-2 error-2]
...
[key-n predicate-n error-n])</pre>
</div>
<ul>
<li>讓我們直接看個範例</li>
</ul>
</article>
</slide>
</slide>
<slide id="sec-" >
<hgroup class="">
<h2 class=" "><div id="outline-container-org081ca57" class="outline-3">
<h3 id="org081ca57">Valip 函數庫:validate</h3>
</div>
</h2>
<h3></h3>
</hgroup>
<article class="" id="text-">
<div class="org-src-container">
<pre class="prettyprint" data-lang="clojurescript">
(validate {:email "[email protected]" :password "weak1"}
[:email present? "Email can't be empty"]
[:email email-address? "Invalid email format"]
[:password present? "Password can't be empty"]
[:password (matches *re-password*) "Invalid password format"])</pre>
</div>
<ul>
<li>在這之中我們驗證 <code>:email</code> 與 <code>:password</code></li>