This repository was archived by the owner on Oct 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.html
More file actions
1603 lines (1259 loc) · 112 KB
/
index.html
File metadata and controls
1603 lines (1259 loc) · 112 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv='X-UA-Compatible' content='IE=edge;chrome=1' />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="google-site-verification" content="qjqVOvRhHfsuDbLOvaAnj2mkUZJ9Xwzg84TMVXduvUc" />
<meta charset="UTF-8">
<title>Hyperloop - Tutorials, Videos & Quickstarts</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/react@15/dist/react.min.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.min.js"></script>
<!-- Opal and Hyperloop -->
<script src="http://cdn.opalrb.org/opal/current/opal.min.js"></script>
<script src="https://rawgit.com/ruby-hyperloop/hyperloop-js/master/dist/opal.min.js"></script>
<script src="https://rawgit.com/ruby-hyperloop/hyperloop-js/master/dist/hyperloop.min.js"></script>
<script src="https://rawgit.com/ruby-hyperloop/hyperloop-js/master/dist/hyperloop-compiler.min.js"></script>
<!-- If you want local copies... -->
<!-- <script src="../../../javascripts/opal-compiler.js"></script> -->
<!-- <script src="../../../javascripts/hyperloop.js"></script> -->
<script src="https://npmcdn.com/tether@1.2.4/dist/js/tether.min.js"></script>
<script src="../../../javascripts/bootstrap.min.js"></script>
<script src="../../../javascripts/codemirror.js"></script>
<script src="../../../javascripts/ruby.js"></script>
<script src="../../../javascripts/matchbrackets.js"></script>
<script src="../../../javascripts/react_player.js"></script>
<script src="../../../javascripts/highlight.pack.js"></script>
<!-- Components are compiled by Hyperloop Express -->
<script type="text/ruby">
class CodeMirror < Hyperloop::Component
param :code, type: String
param :heading, default: "Code"
param :rows, type: Integer, default: 0
param :top_level_component, type: String
before_mount do
r=rand(2**256).to_s(36)[0..7]
@div_code = "code_#{r}"
@div_result = "result_#{r}"
end
after_mount do
# puts params.code
@editor = `CodeMirror(document.getElementById(#{@div_code}), {
value: #{params.code.to_s},
mode: 'text/x-ruby',
matchBrackets: true,
lineNumbers: false,
indentUnit: 2,
theme: 'github'
});`
`#{@editor}.on('change', #{lambda {on_change} })`
`#{@editor}.setSize(null, #{@editor}.defaultTextHeight()*#{params.rows})` unless params.rows == 0
execute_code
end
render(DIV) do
div.card {
div.card_header { params.heading }
div(id: @div_code)
div.card_header do
img(src: '../../../images/hyperloop-logo-small-white.png' , width:'25')
span { ' ' }
'Live editor results'
end
div.card_block(id: @div_result)
}
end
def on_change
execute_code
end
def execute_code
begin
code = `#{@editor}.getValue()`
# puts code
code += "\nElement['##{@div_result}'].render(#{params.top_level_component})"
compiled_code = Opal::Compiler.new(code).compile
`ReactDOM.unmountComponentAtNode(document.getElementById(#{@div_result}));`
# Dispatchers and Receivers example works but Steps example breaks
# Hyperloop::Context.reset!
# `eval(#{compiled_code})`
# Hyperloop::Application::Boot.run()
# Steps example works but Dispatchers and Receivers example breaks
`eval(#{compiled_code})`
Hyperloop::Context.reset!
Hyperloop::Application::Boot.run()
component = Module.const_get params.top_level_component
# we need to see if the component is valid - try checking if it can render static markup
# the following line generates: `undefined method to_n for SimpleComponent`
# if React.render_to_static_markup( component ).empty?
# invalid_component_message
# end
rescue Exception => e
@time_out = after(0.1) do
unable_to_compile_message e.message
end
end
end
def invalid_component_message
message = div.text_danger do
h3.text_danger {"Oops, invalid Component..."}
p { "Your Component has been rejected by React. A valid Component must have a render macro and return just one HTML element." }
end
Element["##{@div_result}"].render{ message }
end
def unable_to_compile_message reason
message = div.text_danger do
h3.text_danger {"Can't compile..."}
p { reason }
end
Element["##{@div_result}"].render{ message }
end
end
</script>
<script type="text/ruby">
Document.ready? do
Element.find('div.codemirror-live-edit').each do |mount_point|
heading = mount_point.attr('data-heading')
rows = mount_point.attr('data-rows')
top_level_component = mount_point.attr('data-top-level-component')
code = Element[mount_point].find('pre').text.strip
params = {code: code, top_level_component: top_level_component}
params = params.merge({heading: heading}) if heading
params = params.merge({rows: rows.to_i}) if rows
codemirror_component = Object.const_get('CodeMirror')
React.render(React.create_element(codemirror_component, params ), mount_point)
end
end
</script>
<script type="text/ruby">
class ToggleCodemirror < Hyperloop::Component
param :code, type: String
param :heading, default: "Code"
param :rows, type: Integer, default: 0
param :top_level_component, type: String
param :show_code
before_mount do
r=rand(2**256).to_s(36)[0..7]
@div_code = "code_#{r}"
@div_result = "result_#{r}"
end
after_mount do
# puts params.code
@editor = `CodeMirror(document.getElementById(#{@div_code}), {
value: #{params.code.to_s},
mode: 'text/x-ruby',
matchBrackets: true,
lineNumbers: false,
indentUnit: 2,
readOnly: true,
theme: 'github'
});`
`#{@editor}.setSize(null, #{@editor}.defaultTextHeight()*#{params.rows})` unless params.rows == 0
Element['.codediv'].hide('')
mutate.show_code false
end
render(DIV) do
div.card {
div.card_header do
div(class: 'row') do
div(class: 'col-md-6') do
img(src: '../../../images/hyperloop-logo-small-white.png' , width:'25')
span { ' ' }
span { params.heading }
end
div(class: 'col-md-6 align-right') do
toggle_link
end
end
end
div(id: @div_code, class: 'codediv')
}
end
def toggle_link
BUTTON(class: 'btn btn-info btn-sm') do
state.show_code ? "Click to hide code" : "Click to show code"
end.on(:click) do |ev|
mutate.show_code !state.show_code
state.show_code ? Element["##{@div_code}"].show('') : Element["##{@div_code}"].hide('')
end
end
end
</script>
<script type="text/ruby">
Document.ready? do
Element.find('div.togglecode').each do |mount_point|
heading = mount_point.attr('data-heading')
rows = mount_point.attr('data-rows')
top_level_component = mount_point.attr('data-top-level-component')
code = Element[mount_point].find('pre').text.strip
params = {code: code, top_level_component: top_level_component}
params = params.merge({heading: heading}) if heading
params = params.merge({rows: rows.to_i}) if rows
codemirror_component = Object.const_get('ToggleCodemirror')
React.render(React.create_element(codemirror_component, params ), mount_point)
end
end
</script>
<link href="../../../stylesheets/bootstrap.min.css" rel="stylesheet" />
<link href="../../../stylesheets/typography.css" rel="stylesheet" />
<link href="../../../stylesheets/override.css" rel="stylesheet" />
<link href="../../../stylesheets/divtable.css" rel="stylesheet" />
<!-- <link href="../../../stylesheets/code.css" rel="stylesheet" /> -->
<link href="../../../stylesheets/github.css" rel="stylesheet" />
<link href="../../../stylesheets/highlighting.css" rel="stylesheet" />
<link href="../../../stylesheets/codemirror.css" rel="stylesheet" />
<link href="../../../stylesheets/monokai-sublime.css" rel="stylesheet" />
<link rel="alternate" type="application/atom+xml" title="Atom Feed" href="/feed.xml" />
<!-- Favicons -->
<link rel="apple-touch-icon" sizes="180x180" href="../../../images/apple-touch-icon.png">
<link rel="icon" type="image/png" href="../../../images/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="../../../images/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="/images/manifest.json">
<link rel="mask-icon" href="../../../images/safari-pinned-tab.svg" color="#e81176">
<meta name="theme-color" content="#ffffff">
</head>
<body>
<div class="navbarbackgroundcolor">
<div class="container">
<nav class="navbar navbar-ligh">
<button class="navbar-toggler hidden-sm-up" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar2" aria-controls="exCollapsingNavbar2" aria-expanded="false" aria-label="Toggle navigation">
☰
</button>
<div class="collapse navbar-toggleable-xs" id="exCollapsingNavbar2">
<ul class="nav navbar-nav">
<li class="nav-item">
<a href="/" class="nav-link"><span class='navfirstletter'>H</span>yperloop</a>
</li>
<li class="nav-item">
<a href="/start/components" class="nav-link active"><span class='navfirstletter'>S</span>tart</a>
</li>
<li class="nav-item">
<a href="/installation" class="nav-link"><span class='navfirstletter'>I</span>nstallation</a>
</li>
<li class="nav-item">
<a href="/tutorials" class="nav-link"><span class='navfirstletter'>T</span>utorials</a>
</li>
<li class="nav-item">
<a href="/gems" class="nav-link"><span class='navfirstletter'>G</span>ems</a>
</li>
<li class="nav-item">
<a href="https://github.com/ruby-hyperloop" class="nav-link"><span class='navfirstletter'>G</span>ithub</a>
</li>
<li class="nav-item">
<a href="/tools" class="nav-link"><span class='navfirstletter'>T</span>ools</a>
</li>
<li class="nav-item">
<a href="/docs/architecture" class="nav-link"><span class='navfirstletter'>D</span>ocs</a>
</li>
<li class="nav-item">
<a href="/help" class="nav-link"><span class='navfirstletter'>H</span>elp</a>
</li>
<li class="nav-item">
<a href="/blog" class="nav-link"><span class='navfirstletter'>B</span>log</a>
</li>
<!-- <li class="nav-item">
<input type="text" name="search" />
</li> -->
</ul>
</div>
</nav>
</div>
</div>
<div class="jumbotron page-header">
<div class="container">
<div class="row hidden-sm-down">
<div class="col-md-2">
<div class="hyperlooplogo">
</div>
</div>
<div class="col-md-8">
<h1 class="display-4 project-name">Hyperloop</h1>
<h4 class="display-7 project-tagline">
Tutorials, screencasts & videos
</h4>
</div>
</div>
<div class="row hidden-md-up">
<div class="col-md-3">
<div class="hyperlooplogo">
</div>
</div>
<div class="col-md-9">
<h1 class="h1 project-name center-text">Hyperloop</h1>
</br>
<h2 class="h5 project-tagline center-text">
Tutorials, screencasts & videos
</h2>
</div>
</div>
</div>
</div>
<div class="page-header-underline">
<div class="container">
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-3 sidenavcol">
<ul class="nav">
<li class="nav-item"><strong><a href="/tutorials/hyperloopcomps">Hyperloop COMPS</a></strong></li>
<ul class="nav">
<li class="nav-item"><a href="/tutorials/hyperloopcomps/compstoreop-first">Simple COMPS App</a></li>
</ul>
<br>
<li class="nav-item"><strong><a href="/tutorials/hyperloopjs">Hyperloop.js</a></strong></li>
<ul class="nav">
<li class="nav-item"><a href="/tutorials/hyperloopjs/helloworld">Hello World</a></li>
<li class="nav-item"><a href="/tutorials/hyperloopjs/chatapp">Chat App</a></li>
</ul>
<br>
<li class="nav-item"><strong><a href="/tutorials/hyperlooprails">Hyperloop and Rails</a></strong></li>
<ul class="nav">
<li class="nav-item"><a href="/tutorials/hyperlooprails/helloworld">Hello World</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/todo-tutorial">Todo MVC</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/chatapp">Chat App</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/hyperloop-devise-tutorial">Devise</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/eventsplanner">Events planner</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/fivelettergame">Five letter word game</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/cloud9">Hyperloop and Cloud9</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/webpack">NPM and Webpack</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/hyperloop-rails-webpackergem-helloworld">Webpacker GEM</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/opalhotreloader">Opal Hot Reloader</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/productionmode">Production mode</a></li>
</ul>
<br>
<li class="nav-item"><strong><a href="/tutorials/hyperloopdeploy">Hyperloop deployment</a></strong></li>
<ul class="nav">
<li class="nav-item"><a href="/tutorials/hyperloopdeploy/deployvps">Deploying on VPS</a></li>
<li class="nav-item"><a href="/tutorials/hyperloopdeploy/deployheroku">Deploying on Heroku</a></li>
</ul>
<br>
<li class="nav-item"><strong><a href="/tutorials/opal">Opal</a></strong></li>
<br>
<li class="nav-item"><strong><a href="/tutorials/videos">Videos</a></strong></li>
</ul>
<div class="getstarted">
<br>
<hr>
<p>NEWS: RubyHyperloop is to be renamed Hyperstack. Come and see the work-in-progress site: </p>
<!-- <button type="button" class="btn btn-primary btn-lg btn-hyperlooppink" onclick="https://hyperstack.org';">https://hyperstack.org</button> -->
<a href='https://hyperstack.org'>https://hyperstack.org</a>
</div>
<br>
</div>
<div class="col-md-9 main-content">
<h2 id="chat-app-tutorial"><i class="flaticon-professor-teaching"></i><span class="bigfirstletter">C</span>hat-App Tutorial</h2>
<p>We'll be building a simple but realistic chat application, a basic version of a chat room offered by a service like gitter.im.</p>
<p><img src="../../../images/tutorials/HyperloopJS-Chatapp.gif" class="imgborder"></p>
<p>You can find the complete source code of this tutorial here: </p>
<p><button type="button" class="btn btn-primary btn-lg btn-hyperlooptrace" onclick="location.href='https://github.com/ruby-hyperloop/hyperloop-js-chatapp';">Hyperloop.js ChatApp Source code</button></p>
<p>Our Chat app will provide:</p>
<ul>
<li>A login window to register your chat handle</li>
<li>A view of the current chat conversation</li>
<li>An input box to submit a chat</li>
</ul>
<p>It will also have a few neat features:</p>
<ul>
<li><strong>Live updates:</strong> other users' chats are popped into the conversation view in real time.</li>
<li><strong>Dynamic Time Formatting:</strong> the time format changes as that chat ages.</li>
<li><strong>Markdown formatting:</strong> users can use Markdown to format their text.</li>
<li><strong>Uses Bootstrap:</strong> for styling</li>
</ul>
<h3 id="tutorial">Tutorial</h3>
<p>To set up your <strong>Hyperloop</strong> environment and continue this tutorial, please first follow the <a href="/installation#hyperloopjssetup">Hyperloop.js installation</a> tutorial.</p>
<h5 id="step-1-preparation">Step 1 - Preparation</h5>
<p>After following the installation tutorial, you should have a HTML file looking like this:</p>
<pre class="highlight html"><code><span class="c"><!-- hyperloopjs-chatapp.html --></span>
<span class="cp"><!DOCTYPE html></span>
<span class="nt"><html></span>
<span class="nt"><head></span>
<span class="nt"><meta</span> <span class="na">charset=</span><span class="s">"UTF-8"</span> <span class="nt">/></span>
<span class="nt"><title></span>Hyperloop Chat App<span class="nt"></title></span>
<span class="c"><!-- React and JQuery --></span>
<span class="nt"><script </span><span class="na">src=</span><span class="s">"https://unpkg.com/react@15/dist/react.min.js"</span><span class="nt">></script></span>
<span class="nt"><script </span><span class="na">src=</span><span class="s">"https://unpkg.com/react-dom@15/dist/react-dom.min.js"</span><span class="nt">></script></span>
<span class="nt"><script </span><span class="na">src=</span><span class="s">"https://code.jquery.com/jquery-2.1.4.min.js"</span><span class="nt">></script></span>
<span class="c"><!-- Opal and Hyperloop --></span>
<span class="nt"><script </span><span class="na">src=</span><span class="s">"https://rawgit.com/ruby-hyperloop/hyperloop-js/master/opal-compiler.min.js"</span><span class="nt">></script></span>
<span class="nt"><script </span><span class="na">src=</span><span class="s">"https://rawgit.com/ruby-hyperloop/hyperloop-js/master/hyperloop.min.js"</span><span class="nt">></script></span>
<span class="nt"></head></span>
<span class="nt"><body></span>
<span class="nt"><script </span><span class="na">type=</span><span class="s">"text/ruby"</span><span class="nt">></span>
<span class="kr">class</span> <span class="nx">Helloworld</span> <span class="o"><</span> <span class="nx">Hyperloop</span><span class="p">::</span><span class="nx">Component</span>
<span class="nx">def</span> <span class="nx">render</span>
<span class="nx">DIV</span> <span class="k">do</span>
<span class="s2">"hello world !"</span>
<span class="nx">end</span>
<span class="nx">end</span>
<span class="nx">end</span>
<span class="nt"></script></span>
<span class="nt"><div</span> <span class="na">data-hyperloop-mount=</span><span class="s">"Helloworld"</span>
<span class="na">data-name=</span><span class="s">""</span><span class="nt">></span>
<span class="nt"></div></span>
<span class="nt"></body></span>
<span class="nt"></html></span>
</code></pre>
<p>We are going to add 3 external libraires needed for our sample ChatApp (Bootstrap and markedown):</p>
<pre class="highlight html"><code><span class="c"><!-- hyperloopjs-chatapp.html --></span>
<span class="nt"><head></span>
...
<span class="nt"><script </span><span class="na">src=</span><span class="s">"https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.min.js"</span><span class="nt">></script></span>
<span class="nt"><script </span><span class="na">src=</span><span class="s">"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"</span><span class="nt">></script></span>
<span class="nt"><link</span> <span class="na">rel=</span><span class="s">"stylesheet"</span> <span class="na">href=</span><span class="s">"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"</span><span class="nt">></span>
<span class="nt"></head></span>
<span class="nt"></html></span>
</code></pre>
<h5 id="step-2-application-structure">Step 2 - Application Structure</h5>
<p>Hyperloop is all about modular, composable components. For our chat app, we'll have the following structure which will be mirrored by 6 corresponding Hyperloop components:</p>
<pre class="highlight plaintext"><code>ChatApp
Nav
Messages
Message
FormattedDiv
Message
FormattedDiv
... repeat for each ... message
InputBox
FormattedDiv
</code></pre>
<p>Each component will render the corresponding portion of the UI and is described by a Ruby class that inherits from Hyperloop::Component.</p>
<p>Let's stub out all of classes right now. Replace the contents inside the
<code><script type="text/ruby">...</script></code> tag with the following ruby code.</p>
<pre class="highlight ruby"><code><span class="k">class</span> <span class="nc">ChatApp</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="no">DIV</span> <span class="k">do</span>
<span class="no">Nav</span><span class="p">()</span>
<span class="no">Messages</span><span class="p">()</span>
<span class="no">InputBox</span><span class="p">()</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Nav</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="no">DIV</span> <span class="p">{</span><span class="s2">"Our Nav Bar Goes Here including a login box"</span><span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Messages</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="c1"># eventually we will loop and display each message</span>
<span class="c1"># for now we will just display one as an example</span>
<span class="no">Message</span><span class="p">()</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Message</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="no">FormattedDiv</span><span class="p">()</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">InputBox</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="no">DIV</span> <span class="k">do</span>
<span class="s2">"An input box to send new messages will"</span><span class="p">.</span><span class="nf">br</span>
<span class="s2">"go here plus a display of the formatted markdown"</span><span class="p">.</span><span class="nf">br</span>
<span class="no">FormattedDiv</span><span class="p">()</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">FormattedDiv</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="s2">"convert and display markdown"</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>Refresh your page, and you should see this:</p>
<pre class="highlight plaintext"><code>Our Nav Bar Goes Here including a login box
convert and display markdown
An input box to send new messages will
go here plus a display of the formatted markdown
convert and display markdown
</code></pre>
<p>Before going on lets understand the basic features of the Hyperloop DSL</p>
<ul>
<li><h6 id="every-component-has-a-render-method">Every component has a <code>render</code> method</h6>
<p>Each component is a class that inherits from <code>Hyperloop::Component</code> and defines a <code>render</code> method.</p></li>
<li><h6 id="the-render-method-must-generate-a-single-react-element">The <code>render</code> method must generate a single <code>React::Element</code></h6>
<p>Look at the <code>ChatApp</code> component: it wraps its three <em>children</em> in a <code>div</code>. If you forgot this wrapper <code>div</code> you would get this error in the browser's javascript console:</p>
<pre class="highlight plaintext"><code>RuntimeError: a components render method must generate and return exactly 1 element or a string
</code></pre></li>
<li><h6 id="html-tags-are-ruby-methods">HTML tags are Ruby methods</h6>
<p>Within the <code>render</code> method each html tag has a corresponding Ruby method. Each of these methods generates a single <code>React::Element</code>, which contains component type plus parameters, plus any nested elements. </p>
<p>All HTML tags have lower case names matching their HTML counter parts.</p></li>
<li><h6 id="application-components-also-define-ruby-methods">Application components also define Ruby methods</h6>
<p>You generate a <code>Message</code> component by calling the <code>Message()</code> method</p>
<p>Because the component class and the component method share the same name you must include either (possibly empty) parenthesis, or block literal following the component name, so Ruby knows you mean to call a method.</p>
<p><code>Message</code> <- a class name
<code>Message()</code> <- generate a <code>Message</code> element.</p></li>
<li><h6 id="by-default-strings-are-wrapped-by-span-tags">By default strings are wrapped by <code>SPAN</code> tags</h6>
<p>Now go all the way down to the <code>FormattedDiv</code> component. If a string appears as the last expression (the returned value) of either a render method or a component's block, the string is wrapped in a <code>SPAN</code> as if you had typed <code>SPAN { "some string" }</code>.</p></li>
<li><h6 id="strings-also-respond-to-br-span-para-th-and-td">Strings also respond to <code>BR, SPAN, PARA, TH</code> and <code>TD</code></h6>
<p>Strings also respond to several element generating methods: <code>BR, SPAN, PARA</code> (for <code>P</code>), <code>TD</code> and <code>TH</code>. Again this is just short hand, so <code>"foo".TD</code> is short for <code>TD { "foo" }</code></p></li>
</ul>
<h5 id="step-3-adding-parameters">Step 3 - Adding Parameters:</h5>
<p>Components have parameters just like methods. Now that we have the basic ChatApp structure, understanding a component's params is a good next step. Lets start by adding the <code>markdown</code> parameter to the <code>FormattedDiv</code> component.</p>
<p>Update the <code>Message, InputBox</code> and <code>FormattedDiv</code> components so they look like this:</p>
<pre class="highlight ruby"><code><span class="k">class</span> <span class="nc">Message</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="no">FormattedDiv</span><span class="p">(</span><span class="ss">markdown: </span><span class="s2">"This **Markdown** will eventually be a message"</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">InputBox</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="no">DIV</span> <span class="k">do</span>
<span class="s2">"An input box to send new messages will"</span><span class="o">.</span><span class="no">BR</span>
<span class="s2">"go here plus a display of the formatted markdown"</span><span class="o">.</span><span class="no">BR</span>
<span class="no">FormattedDiv</span><span class="p">(</span><span class="ss">markdown: </span><span class="s2">"This **Markdown** will get updated as the user types"</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">FormattedDiv</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="n">param</span> <span class="ss">:markdown</span><span class="p">,</span> <span class="ss">type: </span><span class="no">String</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="no">DIV</span> <span class="k">do</span>
<span class="n">params</span><span class="p">.</span><span class="nf">markdown</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<ul>
<li><h6 id="the-param-macro-describes-the-inputs-to-a-component">The <code>param</code> <em>macro</em> describes the <em>inputs</em> to a component.</h6>
<p>In our case the <code>FormattedDiv</code> component will <em>receive</em> the <code>markdown</code> param from either the
<code>Message</code> or <code>InputBox</code> components. </p></li>
<li><h6 id="params-can-have-an-optional-type">Params can have an optional type.</h6>
<p>In our case we declare <code>markdown</code> to be a <code>String</code>. If the incoming parameter does not match the type a warning will be displayed on the console. While typing your params is optional its highly recommended.</p></li>
<li><h6 id="params-are-accessed-using-the-params-object">Params are accessed using the <code>params</code> object.</h6>
<p>Each <code>param</code> you declare in a component will have a corresponding method on the <code>params</code> object. Component parameters are accessible by any instance method within your component using the <code>params</code> object. </p></li>
</ul>
<h5 id="step-4-adding-an-event-handler">Step 4 - Adding An Event Handler:</h5>
<p>Our application will need to respond to three types of events:</p>
<ul>
<li>A user logs in</li>
<li>A user sends a chat</li>
<li>Receiving chat messages.</li>
</ul>
<p>We will start by adding a basic user login handler.</p>
<p>We decided up front that the login box will be part of the top nav bar, so that is where we will add it:</p>
<pre class="highlight ruby"><code><span class="k">class</span> <span class="nc">Nav</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="no">DIV</span> <span class="k">do</span>
<span class="no">INPUT</span><span class="p">(</span><span class="ss">class: :handle</span><span class="p">,</span> <span class="ss">type: :text</span><span class="p">,</span> <span class="ss">placeholder: </span><span class="s2">"Enter Your Handle"</span><span class="p">)</span>
<span class="no">BUTTON</span><span class="p">(</span><span class="ss">type: :button</span><span class="p">)</span> <span class="p">{</span> <span class="s2">"login!"</span> <span class="p">}.</span><span class="nf">on</span><span class="p">(</span><span class="ss">:click</span><span class="p">)</span> <span class="k">do</span>
<span class="n">alert</span><span class="p">(</span><span class="s2">"</span><span class="si">#{</span><span class="no">Element</span><span class="p">[</span><span class="s1">'input.handle'</span><span class="p">].</span><span class="nf">value</span><span class="si">}</span><span class="s2"> logs in!"</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>Replace the <code>Nav</code> component with the above code, and refresh your browser. You should now be able to "login".</p>
<p>Things to notice:</p>
<ul>
<li><h6 id="event-handlers-are-attached-to-any-element-using-the-on-method">Event handlers are attached to any element using the <code>on</code> method.</h6>
<p>The <code>on</code> method takes the event name and a block. The block is called when the event occurs.</p></li>
<li><h6 id="tag-attributes-are-passed-just-like-params">Tag attributes are passed just like params.</h6>
<p><code>INPUT(class: :handle, type: :text, placeholder: "Enter Your Handle")</code><br>
generates<br>
<code><input class="handle", type="text", placeholder="Enter Your Handle" /></code></p></li>
<li><h6 id="strings-and-symbols-are-the-same-type-in-opal">Strings and Symbols are the same type in Opal.</h6>
<p>For effeciency the Opal-Ruby transpiler treats Ruby symbols as strings. So<br>
<code>:text == 'text'</code></p></li>
<li><h6 id="element-is-the-opal-ruby-jquery-compatible-wrapper"><code>Element</code> is the Opal-Ruby jQuery compatible wrapper.</h6>
<p><code>Element['input.handle'].value</code> translates to <code>$('input.handle').value()</code><br>
Be aware that <code>React::Element</code> which we refer to as <em>elements</em> through out the tutorial is not the same as Opal's <code>Element</code> wrapper.</p></li>
</ul>
<h5 id="step-5-adding-state">Step 5 - Adding State:</h5>
<p>Our improved <code>Nav</code> component is still pretty dull, and having to directly access the DOM using <code>Element</code> is not a great idea either.</p>
<p>To add some intelligence to our <code>Nav</code> component it needs <em>state</em>. Hyperloop provides <em>state variables</em> that work like <em>reactive</em> instance variables. When a state variable is updated, it will cause components to re-render. </p>
<p>We are going to add two state variables to our component: <code>current_user_name</code> and <code>user_name_input</code>. </p>
<p><code>current_user_name</code> is either <code>nil</code> (meaning there is no valid user name) or contains the user name string.</p>
<p><code>user_name_input</code> will track the user name as it is typed allowing us to dynamically update the UI based on what the user has typed.</p>
<pre class="highlight ruby"><code><span class="k">class</span> <span class="nc">Nav</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="n">before_mount</span> <span class="k">do</span>
<span class="n">mutate</span><span class="p">.</span><span class="nf">current_user_name!</span> <span class="kp">nil</span>
<span class="n">mutate</span><span class="p">.</span><span class="nf">user_name_input!</span> <span class="s2">""</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="no">DIV</span> <span class="k">do</span>
<span class="no">INPUT</span><span class="p">(</span><span class="ss">type: :text</span><span class="p">,</span> <span class="ss">value: </span><span class="n">state</span><span class="p">.</span><span class="nf">user_name_input</span><span class="p">,</span> <span class="ss">placeholder: </span><span class="s2">"Enter Your Handle"</span>
<span class="p">).</span><span class="nf">on</span><span class="p">(</span><span class="ss">:change</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">e</span><span class="o">|</span>
<span class="n">mutate</span><span class="p">.</span><span class="nf">user_name_input</span> <span class="n">e</span><span class="p">.</span><span class="nf">target</span><span class="p">.</span><span class="nf">value</span>
<span class="k">end</span>
<span class="n">button</span><span class="p">(</span><span class="ss">type: :button</span><span class="p">)</span> <span class="p">{</span> <span class="s2">"login!"</span> <span class="p">}.</span><span class="nf">on</span><span class="p">(</span><span class="ss">:click</span><span class="p">)</span> <span class="k">do</span>
<span class="n">login!</span>
<span class="k">end</span> <span class="k">if</span> <span class="n">valid_new_input?</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">valid_new_input?</span>
<span class="n">state</span><span class="p">.</span><span class="nf">user_name_input</span><span class="p">.</span><span class="nf">present?</span> <span class="o">&&</span> <span class="n">state</span><span class="p">.</span><span class="nf">user_name_input</span> <span class="o">!=</span> <span class="n">state</span><span class="p">.</span><span class="nf">current_user_name</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">login!</span>
<span class="n">mutate</span><span class="p">.</span><span class="nf">current_user_name</span> <span class="n">state</span><span class="p">.</span><span class="nf">user_name_input</span>
<span class="nb">puts</span> <span class="s2">"*** </span><span class="si">#{</span><span class="n">state</span><span class="p">.</span><span class="nf">current_user_name</span><span class="si">}</span><span class="s2"> has logged in"</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>Once again, update the <code>Nav</code> component, and make sure it works. </p>
<p>Things to notice:</p>
<ul>
<li><h6 id="state-variables-are-read-through-the-state-object">State variables are read through the <code>state</code> object.</h6>
<p>Each state variable has a read accessor on the state object:
<code>state.user_name_input</code></p></li>
<li><h6 id="state-variables-are-written-through-the-mutate-object">State variables are written through the <code>mutate</code> object.</h6>
<p>The method <code>mutate</code> will update state and cause re-rendering of the component.</p></li>
<li><h6 id="there-is-no-assignment-method-for-state-variables">There is no <em>assignment</em> method for state variables.</h6>
<p>There are several reasons for this, that we will discuss later, but for now you can
consider the write accessor (or <code>mutate</code> method) to be equivalent to assignment.</p></li>
<li><h6 id="initialize-state-in-the-before_mount-callback">Initialize state in the <code>before_mount</code> callback.</h6>
<p>The <code>before_mount</code> <em>lifecycle</em> callback is called after <code>params</code> are first initialized, but before
render is called, so its the place to initialize your state variables.</p>
<p>More on the <em>lifecycle</em> callbacks later.</p></li>
<li><h6 id="event-handlers-are-passed-the-event-object">Event handlers are passed the event object</h6>
<p>As each character is typed we use the event object to update <code>state.user_name_input</code>. </p>
<p>Besides giving us dynamic access to the state of the user input as its changing, <code>state.user_name_input</code> also frees us from having to tag and interrogate DOM objects directly.</p></li>
<li><h6 id="as-state-changes-the-component-will-rerender">As state changes, the component will rerender.</h6>
<p>For example the login button is only rendered if there is valid new input.</p>
<p>In React you think declaratively about the UI. At any point in time the
<code>render</code> method simply draws the UI based on params and the current state of the
component. As the component state changes, or when it receives new params, <code>render</code>
is called to deliver the updated UI.</p>
<p>Under the hood React.js has efficient algorithms to insure that the minimum DOM
update is performed.</p></li>
<li><h6 id="use-helper-methods-to-keep-render-simple">Use helper methods to keep <code>render</code> simple.</h6>
<p>For example we created the <code>is_valid_new_input?</code> method, and moved the login logic to the <code>login!</code> method.</p>
<p>This helps to understand the core logic and layout of the <code>render</code> method.</p></li>
<li><h6 id="the-puts-method-logs-on-the-js-debug-console">The <code>puts</code> method logs on the js debug console</h6></li>
</ul>
<h5 id="step-6-talking-to-your-parents">Step 6 - Talking to Your Parents</h5>
<p>We will add some other nice features of our <code>Nav</code> component later but right now we need to think about what happens when the user logs in.</p>
<p>We don't want to clutter up our <code>Nav</code> component with this logic so we need some way to communicate that we have a new user name upwards to the parent.</p>
<p>There are quite a few different ways to do this, depending on your specific needs. A straight forward way is for the parent to provide a <em>callback</em> that the <code>Nav</code> component will use to signal that a new user has logged in. </p>
<p>Add the following <code>param</code> declaration at the top of the <code>Nav</code> component:</p>
<pre class="highlight ruby"><code> <span class="n">param</span> <span class="ss">:login</span><span class="p">,</span> <span class="ss">type: </span><span class="no">Proc</span>
</code></pre>
<p>and update the <code>login!</code> method so it looks like this:</p>
<pre class="highlight ruby"><code> <span class="k">def</span> <span class="nf">login!</span>
<span class="n">mutate</span><span class="p">.</span><span class="nf">current_user_name</span> <span class="n">state</span><span class="p">.</span><span class="nf">user_name_input</span>
<span class="n">params</span><span class="p">.</span><span class="nf">login</span><span class="p">(</span><span class="n">state</span><span class="p">.</span><span class="nf">user_name_input</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
<p>Again reload your browser and try logging in. You will notice in the console that there is a new warning:</p>
<pre class="highlight shell_session"><code>Warning: Failed propType: In component `Nav`
Required prop `login` was not specified Check the render method of `ChatApp`.
</code></pre>
<p>Which makes sense, as we specified that <code>Nav</code> wants a <code>login</code> parameter, but <code>App</code> did not provide one. </p>
<p>Lets go ahead and add the callback to the <code>ChatApp</code> component. Add the following method to the bottom of the <code>ChatApp</code>:</p>
<pre class="highlight ruby"><code> <span class="k">def</span> <span class="nf">login</span><span class="p">(</span><span class="n">user_name</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"*** </span><span class="si">#{</span><span class="n">user_name</span><span class="si">}</span><span class="s2"> has logged in"</span>
<span class="k">end</span>
</code></pre>
<p>and pass the <code>login</code> method to the <code>Nav</code> component like so</p>
<pre class="highlight ruby"><code> <span class="k">def</span> <span class="nf">render</span>
<span class="no">Nav</span><span class="p">(</span><span class="ss">login: </span><span class="nb">method</span><span class="p">(</span><span class="ss">:login</span><span class="p">).</span><span class="nf">to_proc</span><span class="p">)</span>
<span class="p">.</span><span class="nf">.</span><span class="p">.</span>
<span class="nf">end</span>
</code></pre>
<p>Reload your browser, and login, and the warning will be gone, and the login message will be back.</p>
<p>Lets review these changes:</p>
<ul>
<li><h6 id="callbacks-are-a-simple-way-to-communicate-upwards">Callbacks are a simple way to communicate upwards</h6>
<p>As we will see there are other mechanisms which are often more appropriate but in this case a call back <code>Proc</code> is the perfect solution. </p>
<p>To implement a callback you declare the param type as <code>Proc</code>. This tells the <code>params</code> object to treat the param as a method call, rather than just returning its value. </p>
<p>Meanwhile in the parent component you will need to pass a <code>Proc</code> to the component. Ruby lets you create and access <code>Proc</code>s in several ways, in our case we converted the instance method <code>login</code> to a <code>Proc</code> using <code>method</code>.</p></li>
<li><h6 id="react-takes-the-approach-of-warning-vs-errors-when-things-go-wrong">React takes the approach of warning vs. errors when things go wrong</h6>
<p>The good thing is this allows you some wiggle room as you are building and testing your components.</p>
<p>The downside is that you need to keep an eye on the console log, and find and remove unexpected warnings.</p></li>
</ul>
<p>Before we go on note that this is an example application. In a real world app we would probably
not use this mechanism for logging on. A real login component would need to check credentials and would
have additional state to track and report the progress of the login process.</p>
<h5 id="step-7-the-chat-service">Step 7 - The Chat Service</h5>
<p>For this tutorial purpose we are going, for now, implement a <strong>Chat service</strong> emulation, all written in Opal. So you just have to include this code into the HTMl page we are working on: </p>
<pre class="highlight ruby"><code><span class="o"><</span><span class="n">script</span> <span class="n">type</span><span class="o">=</span><span class="s2">"text/ruby"</span><span class="o">></span>
<span class="k">class</span> <span class="nc">ChatService</span>
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="o">&</span><span class="n">block</span><span class="p">)</span>
<span class="vi">@block</span> <span class="o">=</span> <span class="n">block</span>
<span class="vi">@messages</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"from"</span><span class="o">=></span><span class="s2">"user1"</span><span class="p">,</span> <span class="s2">"time"</span><span class="o">=></span><span class="mi">1449089985</span><span class="p">,</span> <span class="s2">"message"</span><span class="o">=></span><span class="s2">"A 2 point message: </span><span class="se">\n</span><span class="s2">+ point 1</span><span class="se">\n</span><span class="s2">+ point 2</span><span class="se">\n</span><span class="s2">Got it?"</span><span class="p">},{</span><span class="s2">"from"</span><span class="o">=></span><span class="s2">"user2"</span><span class="p">,</span> <span class="s2">"time"</span><span class="o">=></span><span class="mi">1449262785</span><span class="p">,</span> <span class="s2">"message"</span><span class="o">=></span><span class="s2">"message sent 8 days ago, by user 2"</span><span class="p">},{</span><span class="s2">"from"</span><span class="o">=></span><span class="s2">"user3"</span><span class="p">,</span> <span class="s2">"time"</span><span class="o">=></span><span class="mi">1449521985</span><span class="p">,</span> <span class="s2">"message"</span><span class="o">=></span><span class="s2">"message sent in the last week"</span><span class="p">},{</span><span class="s2">"from"</span><span class="o">=></span><span class="s2">"user2"</span><span class="p">,</span> <span class="s2">"time"</span><span class="o">=></span><span class="mi">1449608385</span><span class="p">,</span> <span class="s2">"message"</span><span class="o">=></span><span class="s2">"message sent **also** in the last week"</span><span class="p">},{</span><span class="s2">"from"</span><span class="o">=></span><span class="s2">"user1"</span><span class="p">,</span> <span class="s2">"time"</span><span class="o">=></span><span class="mi">1449950385</span><span class="p">,</span> <span class="s2">"message"</span><span class="o">=></span><span class="s2">"Was sent within the last hour!"</span><span class="p">},{</span><span class="s2">"from"</span><span class="o">=></span><span class="s2">"user2"</span><span class="p">,</span> <span class="s2">"time"</span><span class="o">=></span><span class="mi">1449952185</span><span class="p">,</span> <span class="s2">"message"</span><span class="o">=></span><span class="s2">"Was sent 30 minutes ago"</span><span class="p">},{</span><span class="s2">"from"</span><span class="o">=></span><span class="s2">"user3"</span><span class="p">,</span> <span class="s2">"time"</span><span class="o">=></span><span class="mi">1449953385</span><span class="p">,</span> <span class="s2">"message"</span><span class="o">=></span><span class="s2">"Was just sent</span><span class="se">\n\n\n\n\n\n\n\n\n</span><span class="s2">with a lot of blanks"</span><span class="p">},{</span><span class="s2">"from"</span><span class="o">=></span><span class="s2">"user1"</span><span class="p">,</span> <span class="s2">"time"</span><span class="o">=></span><span class="mi">1449953985</span><span class="p">,</span> <span class="s2">"message"</span><span class="o">=></span><span class="s2">"just now"</span><span class="p">}</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">login</span><span class="p">(</span><span class="n">user_name</span><span class="p">)</span>
<span class="vi">@user_name</span> <span class="o">=</span> <span class="n">user_name</span>
<span class="vi">@block</span><span class="p">.</span><span class="nf">call</span> <span class="vi">@messages</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">id</span>
<span class="vi">@user_name</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">send</span><span class="p">(</span><span class="n">data</span> <span class="o">=</span> <span class="p">{})</span>
<span class="vi">@messages</span> <span class="o"><<</span> <span class="n">data</span>
<span class="vi">@block</span><span class="p">.</span><span class="nf">call</span> <span class="p">[</span><span class="n">data</span><span class="p">]</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="o"><</span><span class="sr">/script>
</span></code></pre>
<p>As you can see, we have now 2 usefull method: <code>login</code> and <code>send</code>. And the hash <code>messages</code> is initialized with sample data.</p>
<p>Armed with this, we are ready to start displaying chat messages.</p>
<h5 id="step-8-lifecycle-callbacks">Step 8 - Lifecycle Callbacks</h5>
<p>A react component has a well defined life cycle, and your components can hook into the lifecycle using the callback macros. In our application we will use three of the most common callbacks <code>before_mount, after_mount</code> and <code>after_update</code>. </p>
<p>Each call back takes a block or the name of a method, which will be called as the component passes through each stage in its lifecycle.</p>
<p>When our App starts (<strong>mounts</strong> in React terms) we need to initialize the chat service.</p>
<p>Add this code to the beginning of the <code>ChatApp</code> component:</p>
<pre class="highlight ruby"><code> <span class="n">before_mount</span> <span class="k">do</span>
<span class="no">ChatService</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span> <span class="o">|</span> <span class="n">messages</span> <span class="o">|</span>
<span class="k">if</span> <span class="n">state</span><span class="p">.</span><span class="nf">messages</span>
<span class="n">mutate</span><span class="p">.</span><span class="nf">messages</span> <span class="n">state</span><span class="p">.</span><span class="nf">messages</span> <span class="o">+</span> <span class="n">messages</span>
<span class="k">else</span>
<span class="n">mutate</span><span class="p">.</span><span class="nf">messages</span> <span class="n">messages</span>
<span class="k">end</span>
<span class="nb">puts</span> <span class="s2">"state messages updated. state.messages: </span><span class="si">#{</span><span class="n">state</span><span class="p">.</span><span class="nf">messages</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>Before the component mounts our callback will execute and it creates a new <code>ChatService</code> instance. </p>
<p>As each new set of messages arrives the block will execute which will initialize or append the messages to the <code>state.messages</code> state variable. We know we want to make this a state variable since it clearly is going to change asynchronously overtime, and we will want to update the UI when that occurs.</p>
<p>Refresh your browser, and make sure nothing is broken, but notice nothing is changing. <em>Why Not?</em> - because we have not logged in yet.</p>
<h5 id="step-9-instance-and-state-variables">Step 9 - Instance and State Variables</h5>
<p>Okay so while we can initialize the chat service when <code>ChatApp</code> mounts, nothing will happen until the user logs in. </p>
<p>We have our login method already defined, so we just want to change it so that it passes the login to the chat service. To do this we have to save the chat service object in the <code>before_mount</code> callback so that we can use it in the <code>login</code> method.</p>
<p>Change the <code>before_mount</code> callback to be:</p>
<pre class="highlight ruby"><code> <span class="n">before_mount</span> <span class="k">do</span>
<span class="vi">@chat_service</span> <span class="o">=</span> <span class="no">ChatService</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span> <span class="o">|</span> <span class="n">messages</span> <span class="o">|</span>
<span class="n">mutate</span><span class="p">.</span><span class="nf">messages</span> <span class="p">((</span><span class="n">state</span><span class="p">.</span><span class="nf">messages</span> <span class="o">||</span> <span class="p">[])</span> <span class="o">+</span> <span class="n">messages</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"state messages updated. state.messages: </span><span class="si">#{</span><span class="n">state</span><span class="p">.</span><span class="nf">messages</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>Now that we have saved our new <code>ChatService</code> object in <code>@chat_service</code> we can use it in the <code>login</code> method like this:</p>
<pre class="highlight ruby"><code> <span class="k">def</span> <span class="nf">login</span><span class="p">(</span><span class="n">user_name</span><span class="p">)</span>
<span class="vi">@chat_service</span><span class="p">.</span><span class="nf">login</span><span class="p">(</span><span class="n">user_name</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
<p>With this in place refresh your browser, and watch the console as you login. You should see an array of messages that looks like this:</p>
<pre class="highlight shell_session"><code>state messages updated. state.messages: {"from"=>"user1", "time"=>1449089985, "message"=>"A 2 point message: \n+ point 1\n+ point 2\nGot it?"},{"from"=>"user2", "time"=>1449262785, "message"=>"message sent 8 days ago, by user 2"},{"from"=>"user3", "time"=>1449521985, "message"=>"message sent in the last week"},{"from"=>"user2", "time"=>1449608385, "message"=>"message sent **also** in the last week"},{"from"=>"user1", "time"=>1449950385, "message"=>"Was sent within the last hour!"},{"from"=>"user2", "time"=>1449952185, "message"=>"Was sent 30 minutes ago"},{"from"=>"user3", "time"=>1449953385, "message"=>"Was just sent\n\n\n\n\n\n\n\n\nwith a lot of blanks"},{"from"=>"user1", "time"=>1449953985, "message"=>"just now"}
</code></pre>
<p>There is an important point here:</p>
<ul>
<li><h6 id="not-everything-has-to-be-a-state-variable">Not everything has to be a state variable</h6>
<p><code>@chat_service</code> is an instance variable, while <code>state.messages</code> is a state variable.</p>
<p>We can see that as time passes new messages will come in, and we will want to re-render when this happens. This led us to define <code>messages</code> as a state variable. Because <code>messages</code> is a state variable as it changes re-rendering of the ChatApp component will automatically occur.</p>
<p>But so far we have no reason to make <code>@chat_service</code> into a state variable, so we use a plain old instance variable.</p>
<p>For our simple component the messages themselves are the only state we care about.</p>
<p>We will come back to this discussion as we flesh out the rest of our application.</p></li>
</ul>
<p>Now that we are logging in, connecting to the (test) chat service and updating our <code>messages</code> state variable, we are ready to display those messages.</p>
<p>Hopefully by this time you've got a rough idea how we are going to do this!</p>
<p>First update the <code>ChatApp</code> render method like this:</p>
<pre class="highlight ruby"><code> <span class="k">def</span> <span class="nf">render</span>
<span class="no">DIV</span> <span class="k">do</span>
<span class="no">Nav</span> <span class="ss">login: </span><span class="nb">method</span><span class="p">(</span><span class="ss">:login</span><span class="p">).</span><span class="nf">to_proc</span>
<span class="no">Messages</span> <span class="ss">messages: </span><span class="n">state</span><span class="p">.</span><span class="nf">messages</span>
<span class="no">InputBox</span><span class="p">()</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>Then add the corresponding <code>messages</code> parameter to the <code>Messages</code> component:</p>
<pre class="highlight ruby"><code> <span class="n">param</span> <span class="ss">:messages</span><span class="p">,</span> <span class="ss">type: </span><span class="p">[</span><span class="no">Hash</span><span class="p">]</span>
</code></pre>
<p>And update the <code>Messages</code> render method to iterate through all the messages displaying <code>Message</code> for each one:</p>
<pre class="highlight ruby"><code> <span class="k">def</span> <span class="nf">render</span>
<span class="no">DIV</span> <span class="k">do</span>
<span class="n">params</span><span class="p">.</span><span class="nf">messages</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">message</span><span class="o">|</span>
<span class="no">Message</span> <span class="ss">message: </span><span class="n">message</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>Likewise the <code>Message</code> component needs to receive and display a message. Replace the whole component with the following:</p>
<pre class="highlight ruby"><code><span class="k">class</span> <span class="nc">Message</span> <span class="o"><</span> <span class="no">Hyperloop</span><span class="o">::</span><span class="no">Component</span>
<span class="n">param</span> <span class="ss">:message</span><span class="p">,</span> <span class="ss">type: </span><span class="no">Hash</span>
<span class="k">def</span> <span class="nf">render</span>
<span class="no">DIV</span> <span class="k">do</span>
<span class="no">DIV</span> <span class="p">{</span> <span class="n">params</span><span class="p">.</span><span class="nf">message</span><span class="p">[</span><span class="ss">:from</span><span class="p">]</span> <span class="p">}</span>
<span class="no">FormattedDiv</span> <span class="ss">markdown: </span><span class="n">params</span><span class="p">.</span><span class="nf">message</span><span class="p">[</span><span class="ss">:message</span><span class="p">]</span>
<span class="no">DIV</span> <span class="p">{</span> <span class="no">Time</span><span class="p">.</span><span class="nf">at</span><span class="p">(</span><span class="n">params</span><span class="p">.</span><span class="nf">message</span><span class="p">[</span><span class="ss">:time</span><span class="p">]).</span><span class="nf">to_s</span> <span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>