-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
1177 lines (1005 loc) · 96.8 KB
/
atom.xml
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Learn Flight.js]]></title>
<link href="http://learnflight.io/atom.xml" rel="self"/>
<link href="http://learnflight.io/"/>
<updated>2014-03-13T13:26:29-07:00</updated>
<id>http://learnflight.io/</id>
<author>
<name><![CDATA[Rogelio Guzman]]></name>
<email><![CDATA[[email protected]]]></email>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[How to integrate Flight into an existing JavaScript application]]></title>
<link href="http://learnflight.io/blog/2014/03/13/how-to-integrate-flight-into-an-existing-javascript-application/"/>
<updated>2014-03-13T12:56:00-07:00</updated>
<id>http://learnflight.io/blog/2014/03/13/how-to-integrate-flight-into-an-existing-javascript-application</id>
<content type="html"><![CDATA[<p>One of the best things about Flight is that it plays well with exisiting apps. This means that you don’t need to migrate your whole app at once.
Because flight components can’t communicate directly with the outside world, you can gradually migrate new components without affecting the rest of the application.</p>
<p>For this example we are going to use a Color Raffle application, this app consists of three basic parts:</p>
<ol>
<li><strong>Raffle button:</strong> Picks a random color whenever it is clicked.</li>
<li><strong>Color Stats:</strong> Each of the color stats will update everytime that given color comes out of the raffle.</li>
<li><strong>History:</strong> Every time a color is selected, it will be added to the history wall.</li>
</ol>
<p>NOTE: We will not go through the process <a href="https://github.com/flightjs/flight#installation">setting up Flight and its depencencies</a>, but it is a really easy process. For simplicity we are going to use the Flight <a href="https://github.com/flightjs/flight#standalone-version">standalone</a> version.</p>
<p><img class="center small" src="http://learnflight.io/images/color-raffle.png"></p>
<p><a href="https://gist.github.com/rogeliog/9519964">View Source</a> or <a href="http://rogeliog.github.io/learn-flight-color-raffle-demo">Live Demo</a></p>
<!-- more -->
<p>Intentionally, we use a really badly structured JavaScript app. An easy way to start integrating flight is to select one of the three components mentioned above (Raffle button, Color Stats and History) and convert it into a Flight component.</p>
<figure class='code'><figcaption><span>application.js </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">$</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">updateColorStat</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">color</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="s1">'.color-stat'</span><span class="p">).</span><span class="nx">each</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">$node</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">count</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">$node</span><span class="p">.</span><span class="nx">text</span><span class="p">(),</span> <span class="mi">10</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="s1">'color'</span><span class="p">)</span> <span class="o">==</span> <span class="nx">data</span><span class="p">.</span><span class="nx">color</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">4</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="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</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><span class='line'> <span class="kd">var</span> <span class="nx">updateHistory</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">color</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">$history</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'.history'</span><span class="p">);</span>
</span><span class='line'> <span class="nx">$history</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s1">'<li>'</span> <span class="o">+</span> <span class="nx">color</span> <span class="o">+</span> <span class="s1">'</li>'</span><span class="p">);</span>
</span><span class='line'> <span class="p">};</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">var</span> <span class="nx">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'red'</span><span class="p">,</span> <span class="s1">'green'</span><span class="p">,</span> <span class="s1">'blue'</span><span class="p">];</span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="s1">'.raffle-trigger'</span><span class="p">).</span><span class="nx">on</span><span class="p">(</span><span class="s1">'click'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">color</span> <span class="o">=</span> <span class="nx">colors</span><span class="p">[</span><span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="o">*</span><span class="nx">colors</span><span class="p">.</span><span class="nx">length</span><span class="p">)];</span>
</span><span class='line'> <span class="nx">updateColorStat</span><span class="p">(</span><span class="nx">color</span><span class="p">);</span>
</span><span class='line'> <span class="nx">updateHistory</span><span class="p">(</span><span class="nx">color</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>Let’s choose the color stats as a starting point. This is just a function that will update the count of the element which its <code>data-color</code> attribute matches the given color.</p>
<figure class='code'><figcaption><span>updateColorStat </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="kd">var</span> <span class="nx">updateColorStat</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">color</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="s1">'.tweet-stat'</span><span class="p">).</span><span class="nx">each</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">$node</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">count</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">$node</span><span class="p">.</span><span class="nx">text</span><span class="p">(),</span> <span class="mi">10</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="s1">'color'</span><span class="p">)</span> <span class="o">==</span> <span class="nx">data</span><span class="p">.</span><span class="nx">color</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">4</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="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</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></code></pre></td></tr></table></div></figure>
<p>In a new file <code>component/color_stat.js</code> lets create an empty flight component.</p>
<blockquote><p>A Component is just a constructor with properties mixed into its prototype. It comes with a set of basic functionality such as event handling and Component registration.</p></blockquote>
<p>In Flight a component communicates with other components via events, functions are not accessible on the component instance.</p>
<figure class='code'><figcaption><span>components/color_stats.js </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="kd">var</span> <span class="nx">ColorStat</span> <span class="o">=</span> <span class="nx">flight</span><span class="p">.</span><span class="nx">component</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">this</span><span class="p">.</span><span class="nx">after</span><span class="p">(</span><span class="s1">'initialize'</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="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>Now that we have our empty component, lets copy the <code>updateColorStat</code> logic into the component, and update some references</p>
<h2>Color Stats with updateColorStat Flight Component</h2>
<figure class='code'><figcaption><span>components/color_stats.js </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">ColorStat</span> <span class="o">=</span> <span class="nx">flight</span><span class="p">.</span><span class="nx">component</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">this</span><span class="p">.</span><span class="nx">updateColorStat</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">event</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">count</span> <span class="o">=</span> <span class="nb">parseInt</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">text</span><span class="p">(),</span> <span class="mi">10</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="s1">'color'</span><span class="p">)</span> <span class="o">==</span> <span class="nx">data</span><span class="p">.</span><span class="nx">color</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">4</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="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">text</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</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="k">this</span><span class="p">.</span><span class="nx">after</span><span class="p">(</span><span class="s1">'initialize'</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="p">});</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>Great! But how are we going to wire them together? This is the easiest part, we simply want to trigger an event every time the raffle button is clicked.
So, lets replace the call to <code>updateColorStat</code> and trigger an event, <a href="https://blog.twitter.com/2013/flight-at-tweetdeck">see Flight events naming conventions</a>, <code>uiColorSelected</code> and remove updateColorStat.</p>
<figure class='code'><figcaption><span>application.js </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">$</span><span class="p">(</span><span class="s1">'.Raffle-trigger'</span><span class="p">).</span><span class="nx">on</span><span class="p">(</span><span class="s1">'click'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">color</span> <span class="o">=</span> <span class="nx">colors</span><span class="p">[</span><span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="o">*</span><span class="nx">colors</span><span class="p">.</span><span class="nx">length</span><span class="p">)];</span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'uiColorSelected'</span><span class="p">,</span> <span class="p">{</span> <span class="nx">color</span><span class="o">:</span> <span class="nx">color</span> <span class="p">});</span>
</span><span class='line'> <span class="nx">updateHistory</span><span class="p">(</span><span class="nx">color</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>Now lets add a listener to our Flight component.</p>
<figure class='code'><figcaption><span>components/color_stat.js </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="k">this</span><span class="p">.</span><span class="nx">after</span><span class="p">(</span><span class="s1">'initialize'</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">this</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="s1">'uiColorSelected'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">updateColorStat</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>We are going to attach our component to each of the color stats element and done.</p>
<figure class='code'><figcaption><span>page/default.js </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">ColorStat</span><span class="p">.</span><span class="nx">attachTo</span><span class="p">(</span><span class="s1">'.color-stat'</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>
<p>By now we are done migrating our first component, lets continue with the history component. We are going to follow the same steps:</p>
<p>1) Create an empty component.</p>
<figure class='code'><figcaption><span>components/history.js </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">History</span> <span class="o">=</span> <span class="nx">flight</span><span class="p">.</span><span class="nx">component</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">this</span><span class="p">.</span><span class="nx">after</span><span class="p">(</span><span class="s1">'initialize'</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="p">});</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>2) Move logic into the component.</p>
<figure class='code'><figcaption><span>components/history.js </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">History</span> <span class="o">=</span> <span class="nx">flight</span><span class="p">.</span><span class="nx">component</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">this</span><span class="p">.</span><span class="nx">updateHistory</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">event</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s1">'<li>'</span> <span class="o">+</span> <span class="nx">data</span><span class="p">.</span><span class="nx">color</span> <span class="o">+</span> <span class="s1">'</li>'</span><span class="p">);</span>
</span><span class='line'> <span class="p">};</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">after</span><span class="p">(</span><span class="s1">'initialize'</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="p">});</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>3) Listen to desired event.</p>
<figure class='code'><figcaption><span>components/history.js </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="k">this</span><span class="p">.</span><span class="nx">after</span><span class="p">(</span><span class="s1">'initialize'</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">this</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="s1">'uiColorSelected'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">updateHistory</span><span class="p">);</span>
</span><span class='line'> <span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>4) Attach component to element</p>
<figure class='code'><figcaption><span>page/default.js </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">ColorStat</span><span class="p">.</span><span class="nx">attachTo</span><span class="p">(</span><span class="s1">'.color-stat'</span><span class="p">);</span>
</span><span class='line'><span class="nx">History</span><span class="p">.</span><span class="nx">attachTo</span><span class="p">(</span><span class="s1">'.history'</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>
<p>5) Remove logic from legacy code.</p>
<figure class='code'><figcaption><span>application.js </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">$</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'red'</span><span class="p">,</span> <span class="s1">'green'</span><span class="p">,</span> <span class="s1">'blue'</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="s1">'.Raffle-trigger'</span><span class="p">).</span><span class="nx">on</span><span class="p">(</span><span class="s1">'click'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">color</span> <span class="o">=</span> <span class="nx">colors</span><span class="p">[</span><span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="o">*</span><span class="nx">colors</span><span class="p">.</span><span class="nx">length</span><span class="p">)];</span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'uiColorSelected'</span><span class="p">,</span> <span class="p">{</span> <span class="nx">color</span><span class="o">:</span> <span class="nx">color</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>By now we have already moved two of the three components, I will let you do the last one on your own, but it basically the same step.</p>
<p>This is how it might look.</p>
<figure class='code'><figcaption><span>components/raffle_trigger.js </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">RaffleTrigger</span> <span class="o">=</span> <span class="nx">flight</span><span class="p">.</span><span class="nx">component</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">this</span><span class="p">.</span><span class="nx">defaultAttrs</span><span class="p">({</span>
</span><span class='line'> <span class="nx">colors</span><span class="o">:</span> <span class="p">[</span><span class="s1">'red'</span><span class="p">,</span> <span class="s1">'green'</span><span class="p">,</span> <span class="s1">'blue'</span><span class="p">]</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">selectColor</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">event</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">color</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attr</span><span class="p">.</span><span class="nx">colors</span><span class="p">[</span><span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span> <span class="o">*</span> <span class="k">this</span><span class="p">.</span><span class="nx">attr</span><span class="p">.</span><span class="nx">colors</span><span class="p">.</span><span class="nx">length</span><span class="p">)];</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'uiColorSelected'</span><span class="p">,</span> <span class="p">{</span> <span class="nx">color</span><span class="o">:</span> <span class="nx">color</span> <span class="p">});</span>
</span><span class='line'> <span class="p">};</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">after</span><span class="p">(</span><span class="s1">'initialize'</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">this</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'click'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">selectColor</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>You can find the full code <a href="https://gist.github.com/rogeliog/9519964">here</a>.</p>
<p>So, what have we achieved? We have divided the responsibilities across different components that are completely decoupled from each other. Scaling this app is now easier and more maintainable due to the modular nature of Flight.</p>
<p>The three components are UI components, but in the next post we are going to see how to extract a data component that handles all the data logic and provide it to the ui components.</p>
<p><em>NOTE: English is not my native language, I’ll be more than happy to receive any grammar or spelling corrections :)</em></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Testing a Flight data component]]></title>
<link href="http://learnflight.io/blog/2013/10/01/testing-a-flight-data-component/"/>
<updated>2013-10-01T09:53:00-07:00</updated>
<id>http://learnflight.io/blog/2013/10/01/testing-a-flight-data-component</id>
<content type="html"><![CDATA[<p>Testing components in Flight is damn fun. This is because, by nature, they are fully encapsulated and decoupled from the rest of your application.</p>
<p>As of today the best way of testing Flight is with <a href="https://github.com/flightjs/jasmine-flight">jasmine-flight</a> or <a href="https://github.com/flightjs/mocha-flight">mocha-flight</a>, which are extensions to Jasmine and Mocha respectively.</p>
<p>For this post we are going to use a really simple to-do app. A to-do app will generally need a component that handles the data, which has the responsibility of adding/removing items, and notifying other components about that.</p>
<h2>Flight testing principles (optional)</h2>
<p>Flight components communicate via events, so we are going focus on testing only events and not the methods inside the component.</p>
<p>Here is a general rule of thumb for testing a Flight component.</p>
<!-- more -->
<h4>Do test</h4>
<ul>
<li><strong>Event listeners</strong>: Any event that the component is listening to.</li>
<li><strong>Event triggers</strong>: Any event that the component can trigger.</li>
<li><strong>defaultAttrs</strong>: Customization via defaultAttrs.</li>
<li><strong>Initialization</strong>: Special events that are triggered after initialization.</li>
</ul>
<h4>Don’t test</h4>
<ul>
<li><strong>Methods</strong>: The internal methods that are called when listening or triggering events.</li>
<li><strong>Internal state</strong>: Any component state that is not part of the <code>this.attr</code> property.</li>
</ul>
<h3>Setup</h3>
<p>In this case we are going to be using jasmine-flight, you might want to go and <a href="https://github.com/flightjs/jasmine-flight#getting-started">set it up</a>.</p>
<h2>Testing data components</h2>
<p>Some important things to remember about data components are:</p>
<ul>
<li>Don’t test anything related with the DOM.</li>
<li>They shouldn’t know about the UI structure.</li>
</ul>
<p>We have a <code>data/items.js</code> which simulates a backend, it adds/removes to-do items and serves them.</p>
<p>This components is basically doing three things.</p>
<ul>
<li>Listens to the <code>uiNewItemAction</code> event, adds the new item to the list and triggers <code>dataItems</code> to notify that the items were updated.</li>
<li>Listens to the <code>uiRemoveItemAction</code> event, removes the selected item from the list and triggers <code>dataItems</code> to notify that the items were updated.</li>
<li>Triggers the <code>dataItems</code> after initialization to notify components about the initial to-do items.</li>
</ul>
<p>For the sake of simplicity we are going to save the to-do items in an array, but our tests will never know about that, so we can modify it to save on local-storage or in the backend without altering the tests too much.</p>
<p><em>NOTE: For a more robust storage component see <a href="https://github.com/cameronhunter/flight-storage">flight-storage</a>.</em></p>
<figure class='code'><figcaption><span>data/items.js </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">define</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">require</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'> <span class="s1">'use strict'</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">var</span> <span class="nx">defineComponent</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'flight/lib/component'</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="nx">defineComponent</span><span class="p">(</span><span class="nx">items</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">function</span> <span class="nx">items</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">defaultAttrs</span><span class="p">({</span>
</span><span class='line'> <span class="nx">todoItems</span><span class="o">:</span> <span class="p">[]</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">createItem</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">e</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">attr</span><span class="p">.</span><span class="nx">todoItems</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">item</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">triggerDataItems</span><span class="p">();</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">removeItem</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">e</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">attr</span><span class="p">.</span><span class="nx">todoItems</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attr</span><span class="p">.</span><span class="nx">todoItems</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">item</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">return</span> <span class="nx">item</span> <span class="o">!=</span> <span class="nx">data</span><span class="p">.</span><span class="nx">item</span><span class="p">;</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">triggerDataItems</span><span class="p">();</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">triggerDataItems</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'dataItems'</span><span class="p">,</span> <span class="p">{</span> <span class="nx">items</span><span class="o">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">attr</span><span class="p">.</span><span class="nx">todoItems</span> <span class="p">});</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">after</span><span class="p">(</span><span class="s1">'initialize'</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">this</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'uiNewItemAction'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">createItem</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'uiRemoveItemAction'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">removeItem</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">triggerDataItems</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>
<figure class='code'><figcaption><span>spec/data/items.spec.js — TEST FILE </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>
<span class='line-number'>45</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="s1">'use strict'</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">describeComponent</span><span class="p">(</span><span class="s1">'component/data/items'</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="nx">describe</span><span class="p">(</span><span class="s1">'Initialization'</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">it</span><span class="p">(</span><span class="s1">'triggers dataItems with the initial todo items'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">eventSpy</span> <span class="o">=</span> <span class="nx">spyOnEvent</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="s1">'dataItems'</span><span class="p">);</span>
</span><span class='line'> <span class="nx">setupComponent</span><span class="p">({</span> <span class="nx">todoItems</span><span class="o">:</span> <span class="p">[</span><span class="s2">"old item"</span><span class="p">]</span> <span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">expect</span><span class="p">(</span><span class="nx">eventSpy</span><span class="p">).</span><span class="nx">toHaveBeenTriggeredOnAndWith</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">items</span><span class="o">:</span> <span class="p">[</span><span class="s2">"old item"</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><span class='line'> <span class="nx">describe</span><span class="p">(</span><span class="s1">'Listeners'</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="nx">beforeEach</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">setupComponent</span><span class="p">({</span> <span class="nx">todoItems</span><span class="o">:</span> <span class="p">[</span><span class="s2">"item 1"</span><span class="p">,</span> <span class="s2">"item 2"</span><span class="p">]</span> <span class="p">});</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">describe</span><span class="p">(</span><span class="s1">'Listens to uiNewItemAction'</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">it</span><span class="p">(</span><span class="s1">'and triggers dataItems with current item and the new one'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">eventSpy</span> <span class="o">=</span> <span class="nx">spyOnEvent</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="s1">'dataItems'</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">component</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'uiNewItemAction'</span><span class="p">,</span> <span class="p">{</span><span class="nx">item</span><span class="o">:</span> <span class="s2">"new item"</span><span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">expect</span><span class="p">(</span><span class="nx">eventSpy</span><span class="p">).</span><span class="nx">toHaveBeenTriggeredOnAndWith</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">items</span><span class="o">:</span> <span class="p">[</span><span class="s2">"item 1"</span><span class="p">,</span> <span class="s2">"item 2"</span><span class="p">,</span> <span class="s2">"new item"</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><span class='line'> <span class="nx">describe</span><span class="p">(</span><span class="s1">'Listens to uiRemoveItemAction'</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">it</span><span class="p">(</span><span class="s1">'and triggers dataItems without the selected item'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">eventSpy</span> <span class="o">=</span> <span class="nx">spyOnEvent</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="s1">'dataItems'</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">component</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'uiRemoveItemAction'</span><span class="p">,</span> <span class="p">{</span><span class="nx">item</span><span class="o">:</span> <span class="s2">"item 2"</span><span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">expect</span><span class="p">(</span><span class="nx">eventSpy</span><span class="p">).</span><span class="nx">toHaveBeenTriggeredOnAndWith</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">items</span><span class="o">:</span> <span class="p">[</span><span class="s2">"item 1"</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><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>Lets break down this test file and walkthough it.</p>
<figure class='code'><figcaption><span>describeComponent </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="s1">'use strict'</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">describeComponent</span><span class="p">(</span><span class="s1">'component/data/items'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="c1">// more code</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p><a href="https://github.com/flightjs/jasmine-flight#components">describeComponent</a> receives the path of the component that is going to be tested.</p>
<figure class='code'><figcaption><span>Initialization </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">describe</span><span class="p">(</span><span class="s1">'Initialization'</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">it</span><span class="p">(</span><span class="s1">'triggers dataItems with the initial todo items'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">eventSpy</span> <span class="o">=</span> <span class="nx">spyOnEvent</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="s1">'dataItems'</span><span class="p">);</span>
</span><span class='line'> <span class="nx">setupComponent</span><span class="p">({</span> <span class="nx">todoItems</span><span class="o">:</span> <span class="p">[</span><span class="s2">"old item"</span><span class="p">]</span> <span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">expect</span><span class="p">(</span><span class="nx">eventSpy</span><span class="p">).</span><span class="nx">toHaveBeenTriggeredOnAndWith</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">items</span><span class="o">:</span> <span class="p">[</span><span class="s2">"old item"</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></code></pre></td></tr></table></div></figure>
<p><a href="https://github.com/flightjs/jasmine-flight#event-spy">spyOnEvent</a> works for events in the same way that Jasmine’s <a href="http://pivotal.github.io/jasmine/#section-Spies">spyOn</a> works for functions.</p>
<p><a href="https://github.com/flightjs/jasmine-flight#setupcomponent">setupComponent</a> creates an instance of the component, it also sets the instance to <code>this.component</code>. You can pass an object to <code>setupComponent</code> and it will be set as the <code>defaultAttrs</code>.</p>
<p>For this test we are spying on the event that we expect to be triggered, instantiating the components and making sure that the event was triggered with the appropriate data (the items in the to-do list).</p>
<figure class='code'><figcaption><span>Adding new to-do items </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">describe</span><span class="p">(</span><span class="s1">'Listeners'</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="nx">beforeEach</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">setupComponent</span><span class="p">({</span> <span class="nx">todoItems</span><span class="o">:</span> <span class="p">[</span><span class="s2">"item 1"</span><span class="p">,</span> <span class="s2">"item 2"</span><span class="p">]</span> <span class="p">});</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">describe</span><span class="p">(</span><span class="s1">'Listens to uiNewItemAction'</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">it</span><span class="p">(</span><span class="s1">'and triggers dataItems with current item and the new one'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">eventSpy</span> <span class="o">=</span> <span class="nx">spyOnEvent</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="s1">'dataItems'</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">component</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'uiNewItemAction'</span><span class="p">,</span> <span class="p">{</span><span class="nx">item</span><span class="o">:</span> <span class="s2">"new item"</span><span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">expect</span><span class="p">(</span><span class="nx">eventSpy</span><span class="p">).</span><span class="nx">toHaveBeenTriggeredOnAndWith</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">items</span><span class="o">:</span> <span class="p">[</span><span class="s2">"item 1"</span><span class="p">,</span> <span class="s2">"item 2"</span><span class="p">,</span> <span class="s2">"new item"</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><span class='line'> <span class="c1">// more code</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>This test makes sure that the user is able to successfully add new items to the list. We are going to be sharing the call to <code>setupComponent</code> for both tests.</p>
<p>For this test we are spying on the <code>dataItems</code> event and expecting it to be triggered with the two original items (item 1, item 2) and the “new item” after the <code>uiNewItemAction</code> event is triggered.</p>
<figure class='code'><figcaption><span>Removing to-do items </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">describe</span><span class="p">(</span><span class="s1">'Listeners'</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="nx">beforeEach</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">setupComponent</span><span class="p">({</span> <span class="nx">todoItems</span><span class="o">:</span> <span class="p">[</span><span class="s2">"item 1"</span><span class="p">,</span> <span class="s2">"item 2"</span><span class="p">]</span> <span class="p">});</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// more code</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">describe</span><span class="p">(</span><span class="s1">'Listens to uiRemoveItemAction'</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">it</span><span class="p">(</span><span class="s1">'and triggers dataItems without the selected item'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">eventSpy</span> <span class="o">=</span> <span class="nx">spyOnEvent</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="s1">'dataItems'</span><span class="p">);</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">component</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'uiRemoveItemAction'</span><span class="p">,</span> <span class="p">{</span><span class="nx">item</span><span class="o">:</span> <span class="s2">"item 2"</span><span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">expect</span><span class="p">(</span><span class="nx">eventSpy</span><span class="p">).</span><span class="nx">toHaveBeenTriggeredOnAndWith</span><span class="p">(</span><span class="nb">document</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">items</span><span class="o">:</span> <span class="p">[</span><span class="s2">"item 1"</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></code></pre></td></tr></table></div></figure>
<p>This test is opposite of the previous one, it test the removal of items from the list.</p>
<h1>Conclusion</h1>
<p>Note how we never tested any method inside the component; we don’t know anything about its internal structure. This makes refactoring the internals really simple, because no test is bounded to implementation. Actually we don’t even know or care where the items are being saved, they might be saved on localStorage, cookies, in-memory, etc.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[How to create a menu with Flight.js]]></title>
<link href="http://learnflight.io/blog/2013/09/18/how-to-create-a-menu-with-flight-dot-js/"/>
<updated>2013-09-18T15:56:00-07:00</updated>
<id>http://learnflight.io/blog/2013/09/18/how-to-create-a-menu-with-flight-dot-js</id>
<content type="html"><![CDATA[<p>We are going to create a simple navigation menu with <a href="http://flightjs.github.io/">Flight.js</a>. The purpose of this post is to illustrate a practical Flight.js example. Here are the <a href="https://github.com/flightjs/flight/tree/master/doc">docs</a> and the <a href="https://github.com/flightjs/flight">github page</a> if you want to check them out.</p>
<p><img class="center" src="http://learnflight.io/images/navigation-menu.png"></p>
<p><a href="https://github.com/rogeliog/learn-flight-navigation-menu-demo/tree/master/app">View source</a> or <a href="http://rogeliog.github.io/learn-flight-navigation-menu-demo">Live demo</a>.</p>
<!-- more -->
<h2>Initial Setup</h2>
<p>We need to setup some boring stuff to be sure that we all have the same beginning state. The fun stuff starts in the next section.</p>
<p>Before we start be sure to have installed <a href="https://npmjs.org/">npm</a> and <a href="http://bower.io/">bower</a>. Lets create a new folder for our project. Inside our folder lets setup our project. We are going to use the <a href="https://github.com/flightjs/generator-flight">generator-flight</a> yeoman generator to create the basic scaffold for our application.</p>
<p>This will install the appropiate yeoman generator and now we can create our app.</p>
<figure class='code'> <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='bash'><span class='line'><span class="c"># might need sudo</span>
</span><span class='line'>npm install -g generator-flight
</span></code></pre></td></tr></table></div></figure>
<figure class='code'> <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='bash'><span class='line'><span class="c"># NOTE: we don't need Normalize.css or Bootstrap.css</span>
</span><span class='line'>yo flight
</span></code></pre></td></tr></table></div></figure>
<p>Once we have the basic scaffold we can add some markup and styles</p>
<h3>Add the markup</h3>
<figure class='code'><figcaption><span>app/index.html </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='html'><span class='line'><span class="nt"><div</span> <span class="na">id=</span><span class="s">"main"</span><span class="nt">></span>
</span><span class='line'> <span class="nt"><nav</span> <span class="na">id=</span><span class="s">"menu"</span><span class="nt">></span>
</span><span class='line'> <span class="nt"><a</span> <span class="na">href=</span><span class="s">"#"</span> <span class="na">class=</span><span class="s">"home menu-item"</span><span class="nt">></span>Home<span class="nt"></a></span>
</span><span class='line'> <span class="nt"><a</span> <span class="na">href=</span><span class="s">"#"</span> <span class="na">class=</span><span class="s">"projects menu-item"</span><span class="nt">></span>Projects<span class="nt"></a></span>
</span><span class='line'> <span class="nt"><a</span> <span class="na">href=</span><span class="s">"#"</span> <span class="na">class=</span><span class="s">"services menu-item"</span><span class="nt">></span>Services<span class="nt"></a></span>
</span><span class='line'> <span class="nt"><a</span> <span class="na">href=</span><span class="s">"#"</span> <span class="na">class=</span><span class="s">"contact menu-item"</span><span class="nt">></span>Contact<span class="nt"></a></span>
</span><span class='line'> <span class="nt"></nav></span>
</span><span class='line'>
</span><span class='line'> <span class="nt"><div</span> <span class="na">class=</span><span class="s">"menu-content"</span><span class="nt">></span>
</span><span class='line'> Please click a menu item
</span><span class='line'> <span class="nt"></div></span>
</span><span class='line'><span class="nt"></div></span>
</span></code></pre></td></tr></table></div></figure>
<h3>Paste some CSS</h3>
<p>We also need to paste some CSS to make it a look a bit sexier.</p>
<figure class='code'><figcaption><span>app/css/main.css </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='css'><span class='line'><span class="nt">body</span> <span class="p">{</span>
</span><span class='line'> <span class="k">font</span><span class="o">:</span> <span class="m">15px</span><span class="o">/</span><span class="m">1</span><span class="o">.</span><span class="m">3</span> <span class="s1">'Open Sans'</span><span class="o">,</span> <span class="k">sans-serif</span><span class="p">;</span>
</span><span class='line'> <span class="k">color</span><span class="o">:</span> <span class="m">#5e5b64</span><span class="p">;</span>
</span><span class='line'> <span class="k">text-align</span><span class="o">:</span><span class="k">center</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nf">#menu</span> <span class="p">{</span>
</span><span class='line'> <span class="k">display</span><span class="o">:</span> <span class="k">inline</span><span class="o">-</span><span class="k">block</span><span class="p">;</span>
</span><span class='line'> <span class="k">margin</span><span class="o">:</span> <span class="m">60px</span> <span class="k">auto</span> <span class="m">45px</span><span class="p">;</span>
</span><span class='line'> <span class="k">background-color</span><span class="o">:</span> <span class="m">#15a5ec</span><span class="p">;</span>
</span><span class='line'> <span class="n">box</span><span class="o">-</span><span class="n">shadow</span><span class="o">:</span> <span class="m">0</span> <span class="m">1px</span> <span class="m">1px</span> <span class="m">#ccc</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="nf">#menu</span> <span class="nc">.menu-item</span> <span class="p">{</span>
</span><span class='line'> <span class="k">display</span><span class="o">:</span> <span class="k">inline</span><span class="o">-</span><span class="k">block</span><span class="p">;</span>
</span><span class='line'> <span class="k">padding</span><span class="o">:</span> <span class="m">20px</span> <span class="m">30px</span><span class="p">;</span>
</span><span class='line'> <span class="k">color</span><span class="o">:</span> <span class="m">#fff</span><span class="p">;</span>
</span><span class='line'> <span class="k">font-weight</span><span class="o">:</span> <span class="k">bold</span><span class="p">;</span>
</span><span class='line'> <span class="k">font-size</span><span class="o">:</span> <span class="m">16px</span><span class="p">;</span>
</span><span class='line'> <span class="k">text-decoration</span><span class="o">:</span> <span class="k">none</span><span class="p">;</span>
</span><span class='line'> <span class="k">text-transform</span><span class="o">:</span> <span class="k">uppercase</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">transition</span><span class="o">:</span><span class="k">background-color</span> <span class="m">0.20s</span><span class="p">;</span>
</span><span class='line'> <span class="o">-</span><span class="n">moz</span><span class="o">-</span><span class="n">transition</span><span class="o">:</span><span class="k">background-color</span> <span class="m">0.20s</span><span class="p">;</span>
</span><span class='line'> <span class="n">transition</span><span class="o">:</span><span class="k">background-color</span> <span class="m">0.20s</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span><span class="err"></span>
</span><span class='line'>
</span><span class='line'><span class="nf">#menu</span><span class="nc">.home</span> <span class="nc">.home</span><span class="o">,</span>
</span><span class='line'><span class="nf">#menu</span><span class="nc">.projects</span> <span class="nc">.projects</span><span class="o">,</span>
</span><span class='line'><span class="nf">#menu</span><span class="nc">.services</span> <span class="nc">.services</span><span class="o">,</span>
</span><span class='line'><span class="nf">#menu</span><span class="nc">.contact</span> <span class="nc">.contact</span><span class="p">{</span>
</span><span class='line'> <span class="k">background-color</span><span class="o">:</span> <span class="m">#999</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="nc">.menu-content</span> <span class="p">{</span>
</span><span class='line'> <span class="k">font-size</span><span class="o">:</span><span class="m">18px</span><span class="p">;</span>
</span><span class='line'> <span class="k">color</span><span class="o">:</span><span class="m">#fff</span><span class="p">;</span>
</span><span class='line'> <span class="k">padding</span><span class="o">:</span> <span class="m">30px</span> <span class="m">10px</span><span class="p">;</span>
</span><span class='line'> <span class="k">background-color</span><span class="o">:</span><span class="m">#c4d7e0</span><span class="p">;</span>
</span><span class='line'> <span class="k">text-transform</span><span class="o">:</span> <span class="k">uppercase</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<h3>Run the app</h3>
<p>Lets run the app to be sure that everything is wired up properly.</p>
<figure class='code'> <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='bash'><span class='line'><span class="c"># might need sudo</span>
</span><span class='line'>npm install -g http-server
</span><span class='line'>http-server app
</span></code></pre></td></tr></table></div></figure>
<p>Visit <a href="http://localhost:8080">localhost:8080</a></p>
<h2>Create the components</h2>
<p>Now we can start creating our components. In this case we are going to create
three of them.</p>
<ul>
<li>One for the <em>menu title</em>, another for the <em>menu content</em> and third one for <em>generating the data</em> that will be presented in the menu</li>
</ul>
<p>The first two components will be <strong>ui components</strong>, this means that we are going
to attach them to a DOM element and they will manipulate it.</p>
<p>The third component is a <strong>data component</strong>. This component doesn’t know about the DOM state or the menu.
It will only be generating the “backend” response with the appropiate content for
a given section.</p>
<p><strong>ui/menu_item.js</strong> This is the most complex component. It only knows about the menu title</p>
<ul>
<li>Listens to clicks on the <code>.menu-items</code> and triggers a <code>uiNeedsMenuSection</code>
event to notify that we need to change the menu section.</li>
<li>Listens to the <code>dataMenuSection</code> event sent from a data component, adds the appropiate class to highlight the <code>.menu-item</code></li>
</ul>
<figure class='code'><figcaption><span>app/js/component/ui/menu_title.js </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>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">define</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">require</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="s1">'use strict'</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">var</span> <span class="nx">defineComponent</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'flight/lib/component'</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="nx">defineComponent</span><span class="p">(</span><span class="nx">menuItems</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">function</span> <span class="nx">menuItems</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">defaultAttrs</span><span class="p">({</span>
</span><span class='line'> <span class="nx">itemSelector</span><span class="o">:</span> <span class="s1">'.menu-item'</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">highlightSection</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">e</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">$node</span><span class="p">.</span><span class="nx">removeClass</span><span class="p">().</span><span class="nx">addClass</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">section</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">requestSectionChange</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'uiNeedsMenuSection'</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">section</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">).</span><span class="nx">text</span><span class="p">().</span><span class="nx">toLowerCase</span><span class="p">()</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="p">}</span>