forked from zemirco/old-zemirco.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
3951 lines (3045 loc) · 296 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>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>Mirco Zeiss</title>
<meta name="author" content="Mirco Zeiss">
<meta name="description" content="Building a modern and clean signup form still requires more coding than one might think. GitHub
has a simple signup form that I really like. Just …">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://mircozeiss.com">
<link href="/favicon.png" rel="icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<link href="/atom.xml" rel="alternate" title="Mirco Zeiss" type="application/atom+xml">
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="./javascripts/lib/jquery.min.js"%3E%3C/script%3E'))</script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Fjalla+One' rel='stylesheet' type='text/css'>
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="zeMirco">
<meta name="twitter:creator" content="zeMirco">
<meta name="twitter:url" content="http://mircozeiss.com">
<meta name="twitter:title" content="Mirco Zeiss">
<meta name="twitter:description" content="Building a modern and clean signup form still requires more coding than one might think. GitHub
has a simple signup form that I really like. Just three input fields (username, email and password) and …">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-40074158-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body class="collapse-sidebar sidebar-footer" >
<header role="banner"><hgroup>
<h1><a href="/">Mirco Zeiss</a></h1>
</hgroup>
</header>
<nav role="navigation"><ul class="subscription" data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
<form action="http://google.com/search" method="get">
<fieldset role="search">
<input type="hidden" name="q" value="site:mircozeiss.com" />
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
<ul class="main-navigation">
<li><a href="/">Blog</a></li>
<li><a href="/blog/archives">Archives</a></li>
<li><a href="/about.html">About</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div class="blog-index">
<article>
<header>
<h1 class="entry-title"><a href="/github-like-signup-form-with-angularjs-and-bootstrap-v3/">GitHub Like Signup Form With AngularJS and Bootstrap V3</a></h1>
<p class="meta">
<time datetime="2013-08-21T22:37:00+02:00" pubdate data-updated="true">Aug 21<span>st</span>, 2013</time>
| <a href="/github-like-signup-form-with-angularjs-and-bootstrap-v3/#disqus_thread">Comments</a>
</p>
</header>
<div class="entry-content"><p>Building a modern and clean signup form still requires more coding than one might think. <a href="https://github.com/">GitHub</a>
has a simple signup form that I really like. Just three input fields (username, email and password) and you are ready
to go.</p>
<p>In this post we will build a fully featured copy of this signup form. AngularJS manages our frontend and a small Express.js
app takes care of our backend. As an extra gimmick I added password verification to make sure that our users don’t
have typos in their passwords. In the end we have some validation in the frontend and also in our backend. Take a look
at the <a href="http://mysterious-escarpment-9591.herokuapp.com/signup">final app</a>. If everything was correct you should see a json object with your username, email and the hashed password
in addition to some extra information. I also uploaded the whole code to GitHub <a href="https://github.com/zeMirco/ng-signup-form">ng-signup-form</a>.</p>
<h3>The layout</h3>
<p>We use the brand new <a href="http://getbootstrap.com/">Bootstrap v3</a> to build our page. Inside a panel component we have a form with
four input fields. The first one is for username, the second one for email and the last two ones are for password and
password verification.</p>
<p><img src="https://s3.amazonaws.com/mircozeiss.com/signup-form-layout.png" title="Signup form built on top of Bootstrap v3" ></p>
<h3>Backend with Express.js</h3>
<p>Our backend consists of only three routes: <code>GET /signup</code>, <code>POST /signup</code> and <code>POST /signup/check/username</code>. Our main route <code>/</code> redirects
directly to <code>/signup</code> so when you open the page the signup form is immediately shown.</p>
<p>The code for the first route <code>GET /signup</code> looks like the following.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">app</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">'/signup'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">res</span><span class="p">.</span><span class="nx">render</span><span class="p">(</span><span class="s1">'signup'</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>It simply takes our <code>signup.jade</code> template and renders it. Our second route <code>POST /signup</code> is the target for the signup form. Let’s go through the code step by step.
First of all we get the values of the input fields from the request object.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">username</span> <span class="o">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">username</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">email</span> <span class="o">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">email</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">password</span> <span class="o">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">password</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">verification</span> <span class="o">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">verification</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>
<p>Afterwards we perform some input validation, check for duplicate username, create a salt and hashed password
and at the end send a response back to the user. Whenever we encounter invalid data we rerender the
<code>signup.jade</code> template with an additional error message to tell the user what went wrong. Let’s start with some input validation.</p>
<h4>1. Input validation on the backend</h4>
<p>Although our submit button is disabled as long as we don’t have valid values in our input fields, some clever
people will be able to send invalid data to our backend. That’s why we have to double check the incoming data
on the server. We have to make sure that none of the fields are empty, the username doesn’t contain any
non-url-safe characters, email value is a valid email address and finally that the provided password matches
the verification.</p>
<p>All in all the code looks like this.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">error</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
</span><span class='line'><span class="c1">// regexp from https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L4</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">EMAIL_REGEXP</span> <span class="o">=</span> <span class="sr">/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// check for valid inputs</span>
</span><span class='line'><span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">username</span> <span class="o">||</span> <span class="o">!</span><span class="nx">email</span> <span class="o">||</span> <span class="o">!</span><span class="nx">password</span> <span class="o">||</span> <span class="o">!</span><span class="nx">verification</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">error</span> <span class="o">=</span> <span class="s1">'All fields are required'</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">username</span> <span class="o">!==</span> <span class="nb">encodeURIComponent</span><span class="p">(</span><span class="nx">username</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">error</span> <span class="o">=</span> <span class="s1">'Username may not contain any non-url-safe characters'</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">email</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">EMAIL_REGEXP</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">error</span> <span class="o">=</span> <span class="s1">'Email is invalid'</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">password</span> <span class="o">!==</span> <span class="nx">verification</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">error</span> <span class="o">=</span> <span class="s1">'Passwords don\'t match'</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">response</span><span class="p">.</span><span class="nx">status</span><span class="p">(</span><span class="mi">403</span><span class="p">);</span>
</span><span class='line'> <span class="nx">response</span><span class="p">.</span><span class="nx">render</span><span class="p">(</span><span class="s1">'signup'</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">error</span><span class="o">:</span> <span class="nx">error</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="k">return</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<h4>2. Check for duplicate username</h4>
<p>In a real world app you would query your database to check for duplicate usernames. In this example
I’m using an in-memory array with json data as dummy database. We simply loop through the array to see
if an entry with the requested username already exists.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="c1">// dummy db</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">dummyDb</span> <span class="o">=</span> <span class="p">[</span>
</span><span class='line'> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">'john'</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s1">'[email protected]'</span><span class="p">},</span>
</span><span class='line'> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">'jack'</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s1">'[email protected]'</span><span class="p">},</span>
</span><span class='line'> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">'jim'</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s1">'[email protected]'</span><span class="p">},</span>
</span><span class='line'><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// check if username is already taken</span>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">dummyDb</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">dummyDb</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">username</span> <span class="o">===</span> <span class="nx">username</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">response</span><span class="p">.</span><span class="nx">status</span><span class="p">(</span><span class="mi">403</span><span class="p">);</span>
</span><span class='line'> <span class="nx">response</span><span class="p">.</span><span class="nx">render</span><span class="p">(</span><span class="s1">'signup'</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">error</span><span class="o">:</span> <span class="s1">'Username is already taken'</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="k">return</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Pro tip: Never do the same for duplicate email addresses. Read more about this topic in Troy Hunt’s excellent
post <a href="http://www.troyhunt.com/2012/05/everything-you-ever-wanted-to-know.html">Everything you ever wanted to know about building a secure password reset feature</a>.</p>
<h4>3. Create salt and hash password</h4>
<p>Just for completeness and demonstration purpose I included a small function to create a random salt and hash the provided
password. It might help beginners and reminds everyone never to save passwords in plain text to your db.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="c1">// create salt and hash password</span>
</span><span class='line'><span class="nx">pass</span><span class="p">.</span><span class="nx">hash</span><span class="p">(</span><span class="nx">password</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">salt</span><span class="p">,</span> <span class="nx">hash</span><span class="p">){</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// yeah we have a new user</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">user</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">username</span><span class="o">:</span> <span class="nx">username</span><span class="p">,</span>
</span><span class='line'> <span class="nx">email</span><span class="o">:</span> <span class="nx">email</span><span class="p">,</span>
</span><span class='line'> <span class="nx">salt</span><span class="o">:</span> <span class="nx">salt</span><span class="p">,</span>
</span><span class='line'> <span class="nx">hash</span><span class="o">:</span> <span class="nx">hash</span><span class="p">,</span>
</span><span class='line'> <span class="nx">createdAt</span><span class="o">:</span> <span class="nb">Date</span><span class="p">.</span><span class="nx">now</span><span class="p">()</span>
</span><span class='line'> <span class="p">};</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// for fully featured example check duplicate email, send verification link and save user to db</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">response</span><span class="p">.</span><span class="nx">json</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>I used <a href="https://github.com/visionmedia/node-pwd">pwd</a> to generate the salt and hash the password. You need to include the <a href="https://github.com/visionmedia/node-pwd/pull/3/files">pull request</a>
from jlubawy to make this work for node v0.10 and above. An alternative is <a href="https://github.com/ncb000gt/node.bcrypt.js/">bcrypt</a>.</p>
<p>That’s it. We’re are almost done with our backend. I will explain the third route <code>POST /signup/check/username</code> in the section
<strong>Custom validators</strong>.</p>
<h3>Form validation with AngularJS</h3>
<p>A good starting point for form validation with AngularJS is the section in the guide <a href="http://docs.angularjs.org/guide/forms">about forms</a>.
First of all you might want to tell the browser not to validate your form since we’d like to use AngularJS for that. Simply add <code>novalidate</code>
as an attribute to your form.</p>
<p>In our example we use two of the built in validators namely <code>required</code> and <code>email</code>. <code>required</code> makes sure you really enter some content
into the input fields and <code>email</code> uses a regular expression to verify a correct format of the email address. To show error message whenever one
of those validators returns an error we use Bootstrap’s <code>.help-block</code> class in conjunction with the <code>ngShow</code> directive.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">span</span><span class="p">.</span><span class="nx">help</span><span class="o">-</span><span class="nx">block</span><span class="p">(</span><span class="nx">ng</span><span class="o">-</span><span class="nx">show</span><span class="o">=</span><span class="s2">"form.username.$dirty && form.username.$error.required"</span><span class="p">)</span> <span class="nx">Please</span> <span class="nx">choose</span> <span class="nx">a</span> <span class="nx">username</span>
</span><span class='line'><span class="nx">span</span><span class="p">.</span><span class="nx">help</span><span class="o">-</span><span class="nx">block</span><span class="p">(</span><span class="nx">ng</span><span class="o">-</span><span class="nx">show</span><span class="o">=</span><span class="s2">"form.email.$dirty && form.email.$error.email"</span><span class="p">)</span> <span class="nx">Email</span> <span class="nx">is</span> <span class="nx">invalid</span>
</span></code></pre></td></tr></table></div></figure>
<p>You might wonder what <code>$dirty</code> is doing in our code. Every form and every form element in the AngularJS world has two states: <code>$pristine</code> and
<code>$dirty</code>. Your form gets the <code>$pristine</code> state on initial page load before you start entering text into the input fields. So before touching your form it is <code>$pristine</code>. As soon as you start
entering values the forms loses this state and gets the <code>$dirty</code> state because you started working with it. AngularJS also provides CSS classes
to allow for different styling of these states <code>.ng-pristine</code> and <code>.ng-dirty</code>. Read more about that topic in the AngularJS docs about <a href="http://docs.angularjs.org/api/ng.directive:form.FormController">FormController</a>.
If we used the same code as above but without <code>form.username.$dirty</code> and <code>form.email.$dirty</code> the span blocks would be visible on page load.</p>
<h4>Custom validators</h4>
<p>Our first custom validator sends a <code>POST</code> request to the server to check if the username is already taken. We will use a similar
structure as GitHub does. They send a request to <code>/signup_check/username</code> and if the username is already taken they respond with
a status of 403 and the message <code>Username already taken</code>. The same goes for usernames that contain invalid characters.</p>
<p><img src="https://s3.amazonaws.com/mircozeiss.com/github-dev-tools-headers.png" title="Chrome dev tools showing request and response headers" ></p>
<p>Here is the code of our Express app.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">app</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">'/signup/check/username'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">username</span> <span class="o">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">username</span><span class="p">;</span>
</span><span class='line'> <span class="c1">// check if username contains non-url-safe characters</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">username</span> <span class="o">!==</span> <span class="nb">encodeURIComponent</span><span class="p">(</span><span class="nx">username</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">res</span><span class="p">.</span><span class="nx">json</span><span class="p">(</span><span class="mi">403</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">invalidChars</span><span class="o">:</span> <span class="kc">true</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="k">return</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="c1">// check if username is already taken - query your db here</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">usernameTaken</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">dummyDb</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">dummyDb</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">username</span> <span class="o">===</span> <span class="nx">username</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">usernameTaken</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
</span><span class='line'> <span class="k">break</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">usernameTaken</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">res</span><span class="p">.</span><span class="nx">json</span><span class="p">(</span><span class="mi">403</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">isTaken</span><span class="o">:</span> <span class="kc">true</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="k">return</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="c1">// looks like everything is fine</span>
</span><span class='line'> <span class="nx">res</span><span class="p">.</span><span class="nx">send</span><span class="p">(</span><span class="mi">200</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>We first check for non-url-safe characters and afterwards if the username is already taken. If one of those is the case
we send status 403 and some json data back to the client to tell it what went wrong. If everything is fine we simply send
a status of 200 without any data.</p>
<p>On the client side we build a custom directive called <code>unique-username</code>.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">app</span><span class="p">.</span><span class="nx">directive</span><span class="p">(</span><span class="s1">'uniqueUsername'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'$http'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">$http</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">require</span><span class="o">:</span> <span class="s1">'ngModel'</span><span class="p">,</span>
</span><span class='line'> <span class="nx">link</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">scope</span><span class="p">,</span> <span class="nx">elem</span><span class="p">,</span> <span class="nx">attrs</span><span class="p">,</span> <span class="nx">ctrl</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">scope</span><span class="p">.</span><span class="nx">busy</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'> <span class="nx">scope</span><span class="p">.</span><span class="nx">$watch</span><span class="p">(</span><span class="nx">attrs</span><span class="p">.</span><span class="nx">ngModel</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// hide old error messages</span>
</span><span class='line'> <span class="nx">ctrl</span><span class="p">.</span><span class="nx">$setValidity</span><span class="p">(</span><span class="s1">'isTaken'</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
</span><span class='line'> <span class="nx">ctrl</span><span class="p">.</span><span class="nx">$setValidity</span><span class="p">(</span><span class="s1">'invalidChars'</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="c1">// don't send undefined to the server during dirty check</span>
</span><span class='line'> <span class="c1">// empty username is caught by required directive</span>
</span><span class='line'> <span class="k">return</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// show spinner</span>
</span><span class='line'> <span class="nx">scope</span><span class="p">.</span><span class="nx">busy</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// send request to server</span>
</span><span class='line'> <span class="nx">$http</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">'/signup/check/username'</span><span class="p">,</span> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="nx">value</span><span class="p">})</span>
</span><span class='line'> <span class="p">.</span><span class="nx">success</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="c1">// everything is fine -> do nothing</span>
</span><span class='line'> <span class="nx">scope</span><span class="p">.</span><span class="nx">busy</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'> <span class="p">})</span>
</span><span class='line'> <span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// display new error message</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">isTaken</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">ctrl</span><span class="p">.</span><span class="nx">$setValidity</span><span class="p">(</span><span class="s1">'isTaken'</span><span class="p">,</span> <span class="kc">false</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">invalidChars</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">ctrl</span><span class="p">.</span><span class="nx">$setValidity</span><span class="p">(</span><span class="s1">'invalidChars'</span><span class="p">,</span> <span class="kc">false</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">scope</span><span class="p">.</span><span class="nx">busy</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="p">})</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}]);</span>
</span></code></pre></td></tr></table></div></figure>
<p>We can use this directive in the same way we did before with <code>required</code> and <code>email</code> validators.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">span</span><span class="p">.</span><span class="nx">help</span><span class="o">-</span><span class="nx">block</span><span class="p">(</span><span class="nx">ng</span><span class="o">-</span><span class="nx">show</span><span class="o">=</span><span class="s2">"form.username.$dirty && form.username.$error.isTaken"</span><span class="p">)</span> <span class="nx">Username</span> <span class="nx">already</span> <span class="nx">taken</span>
</span><span class='line'><span class="nx">span</span><span class="p">.</span><span class="nx">help</span><span class="o">-</span><span class="nx">block</span><span class="p">(</span><span class="nx">ng</span><span class="o">-</span><span class="nx">show</span><span class="o">=</span><span class="s2">"form.username.$dirty && form.username.$error.invalidChars"</span><span class="p">)</span> <span class="nx">Username</span> <span class="nx">may</span> <span class="nx">not</span> <span class="nx">contain</span> <span class="nx">any</span> <span class="nx">non</span><span class="o">-</span><span class="nx">url</span><span class="o">-</span><span class="nx">safe</span> <span class="nx">characters</span>
</span></code></pre></td></tr></table></div></figure>
<p>The <code>uniqueUsername</code> directive also shows a spinner while talking to the server. Through <code>ng-show="busy"</code> we can hide and show the spinner.
The icon is taken from the Glyphicons that come with Bootstrap and some CSS makes it spin.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='css'><span class='line'><span class="k">@-webkit-keyframes</span> <span class="nt">rotating</span> <span class="p">{</span>
</span><span class='line'> <span class="nt">from</span> <span class="p">{</span>
</span><span class='line'> <span class="o">-</span><span class="n">webkit</span><span class="o">-</span><span class="n">transform</span><span class="o">:</span> <span class="n">rotate</span><span class="p">(</span><span class="m">0</span><span class="n">deg</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="nt">to</span> <span class="p">{</span>
</span><span class='line'> <span class="o">-</span><span class="n">webkit</span><span class="o">-</span><span class="n">transform</span><span class="o">:</span> <span class="n">rotate</span><span class="p">(</span><span class="m">360</span><span class="n">deg</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nc">.rotating</span> <span class="p">{</span>
</span><span class='line'> <span class="k">float</span><span class="o">:</span> <span class="k">right</span><span class="p">;</span>
</span><span class='line'> <span class="k">position</span><span class="o">:</span> <span class="k">relative</span><span class="p">;</span>
</span><span class='line'> <span class="k">top</span><span class="o">:</span> <span class="m">-24px</span><span class="p">;</span>
</span><span class='line'> <span class="k">right</span><span class="o">:</span> <span class="m">10px</span><span class="p">;</span>
</span><span class='line'> <span class="o">-</span><span class="n">webkit</span><span class="o">-</span><span class="n">animation</span><span class="o">:</span> <span class="n">rotating</span> <span class="m">1s</span> <span class="n">linear</span> <span class="n">infinite</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Our second custom form validation method checks whether the password and the password verification input fields have exactly the
same values. Again we use a custom directive for that.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">app</span><span class="p">.</span><span class="nx">directive</span><span class="p">(</span><span class="s1">'match'</span><span class="p">,</span> <span class="p">[</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">require</span><span class="o">:</span> <span class="s1">'ngModel'</span><span class="p">,</span>
</span><span class='line'> <span class="nx">link</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">scope</span><span class="p">,</span> <span class="nx">elem</span><span class="p">,</span> <span class="nx">attrs</span><span class="p">,</span> <span class="nx">ctrl</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">scope</span><span class="p">.</span><span class="nx">$watch</span><span class="p">(</span><span class="s1">'['</span> <span class="o">+</span> <span class="nx">attrs</span><span class="p">.</span><span class="nx">ngModel</span> <span class="o">+</span> <span class="s1">', '</span> <span class="o">+</span> <span class="nx">attrs</span><span class="p">.</span><span class="nx">match</span> <span class="o">+</span> <span class="s1">']'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">){</span>
</span><span class='line'> <span class="nx">ctrl</span><span class="p">.</span><span class="nx">$setValidity</span><span class="p">(</span><span class="s1">'match'</span><span class="p">,</span> <span class="nx">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">===</span> <span class="nx">value</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="p">);</span>
</span><span class='line'> <span class="p">},</span> <span class="kc">true</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}]);</span>
</span></code></pre></td></tr></table></div></figure>
<p>To use this directive apply it to the password input field as well as the verification input field.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">input</span><span class="p">.</span><span class="nx">form</span><span class="o">-</span><span class="nx">control</span><span class="p">(</span><span class="nx">type</span><span class="o">=</span><span class="s2">"password"</span><span class="p">,</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"password"</span><span class="p">,</span> <span class="nx">id</span><span class="o">=</span><span class="s2">"password"</span><span class="p">,</span> <span class="nx">ng</span><span class="o">-</span><span class="nx">model</span><span class="o">=</span><span class="s2">"password"</span><span class="p">,</span> <span class="nx">required</span><span class="p">,</span> <span class="nx">match</span><span class="o">=</span><span class="s2">"verification"</span><span class="p">)</span>
</span><span class='line'><span class="nx">input</span><span class="p">.</span><span class="nx">form</span><span class="o">-</span><span class="nx">control</span><span class="p">(</span><span class="nx">type</span><span class="o">=</span><span class="s2">"password"</span><span class="p">,</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"verification"</span><span class="p">,</span> <span class="nx">id</span><span class="o">=</span><span class="s2">"verification"</span><span class="p">,</span> <span class="nx">ng</span><span class="o">-</span><span class="nx">model</span><span class="o">=</span><span class="s2">"verification"</span><span class="p">,</span> <span class="nx">required</span><span class="p">,</span> <span class="nx">match</span><span class="o">=</span><span class="s2">"password"</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>The error message works as expected but only show it when the verification input fields contains a value.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">span</span><span class="p">.</span><span class="nx">help</span><span class="o">-</span><span class="nx">block</span><span class="p">(</span><span class="nx">ng</span><span class="o">-</span><span class="nx">show</span><span class="o">=</span><span class="s2">"form.verification.$dirty && form.verification.$error.match && !form.verification.$error.required"</span><span class="p">)</span> <span class="nx">Passwords</span> <span class="nx">don</span><span class="err">'</span><span class="nx">t</span> <span class="nx">match</span>
</span></code></pre></td></tr></table></div></figure>
<p>Last but not least we keep the submit button disabled as long as our form contains validation errors.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">input</span><span class="p">.</span><span class="nx">btn</span><span class="p">.</span><span class="nx">btn</span><span class="o">-</span><span class="nx">primary</span><span class="p">(</span><span class="nx">type</span><span class="o">=</span><span class="s2">"submit"</span><span class="p">,</span> <span class="nx">value</span><span class="o">=</span><span class="s2">"Sign up"</span><span class="p">,</span> <span class="nx">ng</span><span class="o">-</span><span class="nx">disabled</span><span class="o">=</span><span class="s2">"form.$invalid"</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<h3>Conclusion</h3>
<p>We’ve built a signup form very similar to the one GitHub uses with an Express.js backend and AngularJS
on the frontend. In addition to the form validators already provided by AngularJS we’ve implemented two
custom form validators for unique usernames and password verification. Take a look at the final <a href="http://mysterious-escarpment-9591.herokuapp.com/signup">app</a>. Try usernames like
<strong>john</strong>, <strong>jack</strong> and <strong>jim</strong>.</p>
<p>One thing I couldn’t achieve is sending invalid usernames and emails back from the server to the client.
So that the input fields already contain the false information and the user just has to correct them. That
doesn’t work because AngularJS doesn’t allow an invalid email inside an input field of type <code>email</code>. To
better understand the problem take a look at a quick <a href="http://plnkr.co/edit/XzcCA7?p=preview">example</a>. If you’ve got an idea how to solve this problem
please feel free to comment below or provide an answer to my question at <a href="http://stackoverflow.com/questions/18217916/how-to-init-invalid-email-and-show-in-inputtype-email">stackoverflow</a>. Thank you!</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/sync-multiple-angularjs-apps-without-server-via-pouchdb/">Sync Multiple AngularJS Apps Without Server via PouchDB</a></h1>
<p class="meta">
<time datetime="2013-07-10T20:34:00+02:00" pubdate data-updated="true">Jul 10<span>th</span>, 2013</time>
| <a href="/sync-multiple-angularjs-apps-without-server-via-pouchdb/#disqus_thread">Comments</a>
</p>
</header>
<div class="entry-content"><p>Nowadays several solutions exist to keep web apps in sync and/or store data offline with JavaScript:</p>
<ul>
<li><a href="http://socket.io/">Socket.io</a></li>
<li><a href="http://www.breezejs.com/">Breeze.js</a></li>
<li><a href="https://towtruck.mozillalabs.com/">TowTruck</a></li>
<li><a href="https://www.firebase.com/">Firebase</a></li>
<li><a href="http://pusher.com/">Pusher</a></li>
<li><a href="https://parse.com/products">Parse</a></li>
<li>and my latest favorite <a href="http://hood.ie/">Hood.ie</a></li>
</ul>
<p>I could go on with that list but you get the idea. Some of the mentioned technologies are more powerful
and have more features out of the box than others. They usually need a server that runs your app and
takes care of all the business logic. In this post I will demonstrate how to build a web app that
syncs data automatically, runs on all modern platforms, stores data persistently and <strong>doesn’t need a server</strong>.
The app is powered by <a href="http://angularjs.org/">AngularJS</a> and <a href="http://pouchdb.com/">PouchDB</a>.</p>
<p>The video shows the final app in action. On the left side we have Chrome and Firefox showing our <code>index.html</code> from file and
on the right side you can see Futon, the admin panel for CouchDB.</p>
<iframe width="640" height="480" src="http://www.youtube.com/embed/QSC1fnfUlU0" frameborder="0" allowfullscreen></iframe>
<br>
<br>
<h3>Install CouchDB</h3>
<p>First of all you have to install CouchDB. You could use some third party instance provider like <a href="https://cloudant.com/">Cloudant</a>
or <a href="http://www.iriscouch.com/">Iris Couch</a>. However our final example won’t work with them because we need to have CORS enabled on our CouchDB.
CORS was introduced with v1.3. Cloudant instances are running on v1.0.2 whereas Iris Couch actually provides
v1.3. Sadly you cannot enable CORS by hand. That’s why we will work with our own instance.</p>
<p>Go to <a href="http://couchdb.apache.org/">couchdb.apache.org</a> and click on the big fat download button. Download
the package for your operating system, select a mirror, wait and then open CouchDB. On my MacBook I get
a little CouchDB icon on my Menu Bar. Click on it and select <strong>Open admin console</strong>. Or just open your browser and navigate to
<a href="http://127.0.0.1:5984/_utils/">127.0.0.1:5984/_utils</a>. You are now inside Futon, which is the admin tool for CouchDB.</p>
<p>First of all we have to <a href="http://docs.couchdb.org/en/latest/cors.html">enable CORS</a>. Go to section <em>httpd</em> and
set <strong>enable_cors</strong> to true. Navigate to the bottom of the page and click on <strong>Add a new section</strong>. Enter the following into
the form that pops up</p>
<ul>
<li>section: cors</li>
<li>option: origins</li>
<li>value: *</li>
</ul>
<p>Now let’s create our database. Click on <strong>Create database …</strong> and enter <code>ng-db</code>. The name is important and we have
to use the exact name later on for the client code. That’s it! Our CouchDB instance is running, CORS is enabled
and we have a database to work with. Next install PouchDB.</p>
<h3>Download PouchDB</h3>
<p>To add PouchDB to your app you simply have to <a href="http://download.pouchdb.com/pouchdb-nightly.min.js">download</a> the JS file. As always I recommend working with the
non-minified version during development and switch to the min version for deployment. Include the .js file inside
your index.html.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><script </span><span class="na">src=</span><span class="s">"pouchdb.js"</span><span class="nt">></script></span>
</span></code></pre></td></tr></table></div></figure>
<p>Done. We can now start using PouchDB in our app.</p>
<h3>Create AngularJS app</h3>
<p>Activate AngularJS as shown in the <a href="http://angularjs.org/">docs</a>.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">myApp</span> <span class="o">=</span> <span class="nx">angular</span><span class="p">.</span><span class="nx">module</span><span class="p">(</span><span class="s1">'myApp'</span><span class="p">,</span> <span class="p">[]);</span>
</span></code></pre></td></tr></table></div></figure>
<h4>PouchDB service</h4>
<p>The first service we create simply makes the PouchDB instance available inside the AngularJS world.
It also turns on continuous replication so changes are automatically synced between database and client
no matter where they occur.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">myApp</span><span class="p">.</span><span class="nx">factory</span><span class="p">(</span><span class="s1">'myPouch'</span><span class="p">,</span> <span class="p">[</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">var</span> <span class="nx">mydb</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Pouch</span><span class="p">(</span><span class="s1">'ng-pouch'</span><span class="p">);</span>
</span><span class='line'> <span class="nx">Pouch</span><span class="p">.</span><span class="nx">replicate</span><span class="p">(</span><span class="s1">'ng-pouch'</span><span class="p">,</span> <span class="s1">'http://127.0.0.1:5984/ng-db'</span><span class="p">,</span> <span class="p">{</span><span class="nx">continuous</span><span class="o">:</span> <span class="kc">true</span><span class="p">});</span>
</span><span class='line'> <span class="nx">Pouch</span><span class="p">.</span><span class="nx">replicate</span><span class="p">(</span><span class="s1">'http://127.0.0.1:5984/ng-db'</span><span class="p">,</span> <span class="s1">'ng-pouch'</span><span class="p">,</span> <span class="p">{</span><span class="nx">continuous</span><span class="o">:</span> <span class="kc">true</span><span class="p">});</span>
</span><span class='line'> <span class="k">return</span> <span class="nx">mydb</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="p">}]);</span>
</span></code></pre></td></tr></table></div></figure>
<h4>PouchDB promises wrapper</h4>
<p>Our second service is a simple wrapper around PouchDB’s native API to save and remove documents. The
service provides two helper functions <code>return()</code> and <code>remove(id)</code> that both return promises. That keeps
the async code nice and tidy.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">myApp</span><span class="p">.</span><span class="nx">factory</span><span class="p">(</span><span class="s1">'pouchWrapper'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'$q'</span><span class="p">,</span> <span class="s1">'$rootScope'</span><span class="p">,</span> <span class="s1">'myPouch'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">$q</span><span class="p">,</span> <span class="nx">$rootScope</span><span class="p">,</span> <span class="nx">myPouch</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">add</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">text</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">deferred</span> <span class="o">=</span> <span class="nx">$q</span><span class="p">.</span><span class="nx">defer</span><span class="p">();</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">doc</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">type</span><span class="o">:</span> <span class="s1">'todo'</span><span class="p">,</span>
</span><span class='line'> <span class="nx">text</span><span class="o">:</span> <span class="nx">text</span>
</span><span class='line'> <span class="p">};</span>
</span><span class='line'> <span class="nx">myPouch</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="nx">doc</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$rootScope</span><span class="p">.</span><span class="nx">$apply</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">deferred</span><span class="p">.</span><span class="nx">reject</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
</span><span class='line'> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">deferred</span><span class="p">.</span><span class="nx">resolve</span><span class="p">(</span><span class="nx">res</span><span class="p">)</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="k">return</span> <span class="nx">deferred</span><span class="p">.</span><span class="nx">promise</span><span class="p">;</span>
</span><span class='line'> <span class="p">},</span>
</span><span class='line'> <span class="nx">remove</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">id</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">deferred</span> <span class="o">=</span> <span class="nx">$q</span><span class="p">.</span><span class="nx">defer</span><span class="p">();</span>
</span><span class='line'> <span class="nx">myPouch</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">id</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">doc</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$rootScope</span><span class="p">.</span><span class="nx">$apply</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">deferred</span><span class="p">.</span><span class="nx">reject</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">myPouch</span><span class="p">.</span><span class="nx">remove</span><span class="p">(</span><span class="nx">doc</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$rootScope</span><span class="p">.</span><span class="nx">$apply</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">deferred</span><span class="p">.</span><span class="nx">reject</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
</span><span class='line'> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">deferred</span><span class="p">.</span><span class="nx">resolve</span><span class="p">(</span><span class="nx">res</span><span class="p">)</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="k">return</span> <span class="nx">deferred</span><span class="p">.</span><span class="nx">promise</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">}]);</span>
</span></code></pre></td></tr></table></div></figure>
<h4>PouchDB event listener</h4>
<p>Our last service is a listener that emits events whenever PouchDB fires the <code>onChange</code> event. It either
emits <code>newTodo</code> when a new document is added to the database or <code>delTodo</code> when a document is deleted. The change
object coming from PouchDB looks like the following</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="s2">"id"</span><span class="o">:</span> <span class="s2">"6F48205D-E97B-4621-ACAD-4CD3DFAC074E"</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"seq"</span><span class="o">:</span> <span class="mi">1</span><span class="p">,</span>
</span><span class='line'> <span class="s2">"changes"</span><span class="o">:</span> <span class="p">[{</span><span class="s2">"rev"</span><span class="o">:</span><span class="s2">"2-96ea3cf93a75c6510c08c95e42686aa1"</span><span class="p">}],</span>
</span><span class='line'> <span class="s2">"deleted"</span><span class="o">:</span> <span class="kc">true</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>From the key <code>deleted</code> we get a Boolean value that tells us if the change was a deletion or an addition
to our database. If the value is <code>true</code> we know that an object was deleted from our database and we therefore
emit a <code>delTodo</code> event with the document id. If the value is <code>false</code> we unfortunately don’t get the new
object directly from the <code>onChange</code> handler. We only get the document id and have to manually get it
via GET request. At the end we fire a <code>newTodo</code> event with the new document as data.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">myApp</span><span class="p">.</span><span class="nx">factory</span><span class="p">(</span><span class="s1">'listener'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'$rootScope'</span><span class="p">,</span> <span class="s1">'myPouch'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">$rootScope</span><span class="p">,</span> <span class="nx">myPouch</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">myPouch</span><span class="p">.</span><span class="nx">changes</span><span class="p">({</span>
</span><span class='line'> <span class="nx">continuous</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
</span><span class='line'> <span class="nx">onChange</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">change</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">change</span><span class="p">.</span><span class="nx">deleted</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$rootScope</span><span class="p">.</span><span class="nx">$apply</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">myPouch</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">change</span><span class="p">.</span><span class="nx">id</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">doc</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$rootScope</span><span class="p">.</span><span class="nx">$apply</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
</span><span class='line'> <span class="nx">$rootScope</span><span class="p">.</span><span class="nx">$broadcast</span><span class="p">(</span><span class="s1">'newTodo'</span><span class="p">,</span> <span class="nx">doc</span><span class="p">);</span>
</span><span class='line'> <span class="p">})</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="p">})</span>
</span><span class='line'> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$rootScope</span><span class="p">.</span><span class="nx">$apply</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$rootScope</span><span class="p">.</span><span class="nx">$broadcast</span><span class="p">(</span><span class="s1">'delTodo'</span><span class="p">,</span> <span class="nx">change</span><span class="p">.</span><span class="nx">id</span><span class="p">);</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">})</span>
</span><span class='line'><span class="p">}]);</span>
</span></code></pre></td></tr></table></div></figure>
<h4>Main controller</h4>
<p>Our controller combines the three services and creates a link to our view. First of all we create
an empty array that will hold our todo objects. The <code>submit()</code> function
is executed whenever the <strong>Add</strong> button is clicked. A click on the small cross calls the <code>remove(id)</code>
function.</p>
<p>At the end of our controller we have the listener for our two custom events <code>newTodo</code> and <code>delTodo</code>.
It adds or removes items from the <code>todos</code> array.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">myApp</span><span class="p">.</span><span class="nx">controller</span><span class="p">(</span><span class="s1">'TodoCtrl'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'$scope'</span><span class="p">,</span> <span class="s1">'listener'</span><span class="p">,</span> <span class="s1">'pouchWrapper'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">$scope</span><span class="p">,</span> <span class="nx">listener</span><span class="p">,</span> <span class="nx">pouchWrapper</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">$scope</span><span class="p">.</span><span class="nx">submit</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">pouchWrapper</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">$scope</span><span class="p">.</span><span class="nx">text</span><span class="p">).</span><span class="nx">then</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$scope</span><span class="p">.</span><span class="nx">text</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span>
</span><span class='line'> <span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">reason</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">reason</span><span class="p">);</span>
</span><span class='line'> <span class="p">})</span>
</span><span class='line'> <span class="p">};</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">$scope</span><span class="p">.</span><span class="nx">remove</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">id</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">pouchWrapper</span><span class="p">.</span><span class="nx">remove</span><span class="p">(</span><span class="nx">id</span><span class="p">).</span><span class="nx">then</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'><span class="c1">// console.log(res);</span>
</span><span class='line'> <span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">reason</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">reason</span><span class="p">);</span>
</span><span class='line'> <span class="p">})</span>
</span><span class='line'> <span class="p">};</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">$scope</span><span class="p">.</span><span class="nx">todos</span> <span class="o">=</span> <span class="p">[];</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">$scope</span><span class="p">.</span><span class="nx">$on</span><span class="p">(</span><span class="s1">'newTodo'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">event</span><span class="p">,</span> <span class="nx">todo</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$scope</span><span class="p">.</span><span class="nx">todos</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">todo</span><span class="p">);</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">$scope</span><span class="p">.</span><span class="nx">$on</span><span class="p">(</span><span class="s1">'delTodo'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">event</span><span class="p">,</span> <span class="nx">id</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span><span class="o"><</span><span class="nx">$scope</span><span class="p">.</span><span class="nx">todos</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">$scope</span><span class="p">.</span><span class="nx">todos</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">_id</span> <span class="o">===</span> <span class="nx">id</span><span class="p">)</span> <span class="p">{</span>