-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
1506 lines (1123 loc) · 60.1 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[benIT's notebook]]></title>
<link href="/atom.xml" rel="self"/>
<link href="/"/>
<updated>2021-10-11T14:01:44+00:00</updated>
<id>/</id>
<generator uri="http://sculpin.io/">Sculpin</generator>
<entry>
<title type="html"><![CDATA[ssh proxy jump]]></title>
<link href="/blog/2021/10/11/ssh-proxy-jump"/>
<updated>2021-10-11T00:00:00+00:00</updated>
<id>/blog/2021/10/11/ssh-proxy-jump</id>
<content type="html"><![CDATA[<h2 id="context">Context</h2>
<p>2 machines:</p>
<ul>
<li>one public, available on internet: <code>bastion-server</code></li>
<li>one private, not available on internet: <code>private-server</code></li>
</ul>
<p>The public machine is allow to connect the private with ssh protocol.</p>
<h2 id="setup-ssh-config">Setup ssh config</h2>
<p>The trick is to make use <code>ProxyJump</code> directive in <code>.ssh/config</code>:</p>
<pre><code>Host bastion-server
User pi
Hostname bastion-server.com
Port 22
Host private-server
User ben
Hostname private-server.com
ProxyJump bastion-server
</code></pre>
<p>NB: public ssh keys must be added to these 2 servers with <code>ssh-copy-id</code> command for instance.</p>
<h2 id="usage">Usage</h2>
<p>We can now execute remote commands on the private server jumping from the bastion server.</p>
<pre><code>ssh private-server hostname
</code></pre>
<p>You can use that to add your computer @IP to the private server. This can be useful when working with a mobile phone as access point:</p>
<pre><code>IP=$(curl -s ifconfig.me) && DATE=$(date +%Y-%m-%d_%T) && echo "adding $IP to ufw at $DATE" && ssh private-server sudo ufw allow from $IP comment ben-sosh-$DATE && ssh private-server sudo ufw status numbered
</code></pre>
<h2 id="source">Source</h2>
<p>https://www.syloe.com/rebonds-ssh/</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[symfonycasts course notes - The Delightful World of Vue]]></title>
<link href="/blog/2021/07/22/sfcast-vuejs"/>
<updated>2021-07-22T00:00:00+00:00</updated>
<id>/blog/2021/07/22/sfcast-vuejs</id>
<content type="html"><![CDATA[<h1 id="about">About</h1>
<p>This is raw notes from the great symfony cast course "The Delightful World of Vue".
Content have been fully copied/pasted from the official <a href="https://symfonycasts.com/screencast/vue">public resource</a>.</p>
<h1 id="notes">Notes</h1>
<p>...</p>
<h2 id="21----where-should-a-piece-of-data-live%3F">21 - Where should a Piece of Data Live?</h2>
<p>https://symfonycasts.com/screencast/vue/data-location#play-
Earlier, I said that you should never, ever change the value of a prop. Props are meant to be read but not modified</p>
<p>What is the purpose of the special mounted() function in Vue?</p>
<p>https://symfonycasts.com/screencast/vue/ajax/activity/376</p>
<p>That's right. It's a special event callback that you can define in your components to do stuff just after a component is mounted in the DOM.</p>
<p>https://symfonycasts.com/screencast/vue/loading/activity/380</p>
<h2 id="28---loading-component">28 - Loading Component</h2>
<p>https://symfonycasts.com/screencast/vue/loading</p>
<p>created will execute as soon as the component is instantiated. Even though the DOM structure still isn't mounted, you can still perform any other operations that deal with reactive data!</p>
<p>mounted, on the other hand, will be executed once the component has been mounted in the DOM. This is useful for any edge case when you need to manipulate the DOM directly!</p>
<h2 id="39---hoisting-data-up">39 - Hoisting Data Up</h2>
<p>https://symfonycasts.com/screencast/vue/hoist-data#play</p>
<p>This is a fairly common situation: you start by putting your data in one component, then later you need to move it higher so that more parts of your app can use it. We did this once earlier: we moved the collapsed boolean data from Sidebar up to Products so that we could use it in more places.</p>
<h2 id="47---watchers%3A-the-good%2C-the-bad-%26-the-useful%21">47 - Watchers: The Good, The Bad & The Useful!</h2>
<p>https://symfonycasts.com/screencast/vue/watcher#play</p>
<p>watcher! Very simply: a watcher is a function that's called by Vue whenever a specific prop or data changes.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[NFS setup]]></title>
<link href="/blog/2021/05/18/nfs-setup"/>
<updated>2021-05-18T00:00:00+00:00</updated>
<id>/blog/2021/05/18/nfs-setup</id>
<content type="html"><![CDATA[<p><a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-an-nfs-mount-on-ubuntu-20-04">Source</a>.</p>
<p>In this article, there are 2 linux machines: <code>desktop</code> & <code>laptop</code>.</p>
<p>The desktop machine can be accessed via @IP or using <code>desktop.local</code> alias.</p>
<h2 id="host">Host</h2>
<h3 id="%C2%A0install"> Install</h3>
<pre><code>sudo apt install nfs-kernel-server
</code></pre>
<p>Configure clients access to host share in <code>/etc/exports</code> :</p>
<pre><code>/home 192.168.1.0/24(rw,sync,no_root_squash,no_subtree_check)
</code></pre>
<p>restart service:</p>
<pre><code> service nfs-kernel-server restart
</code></pre>
<h2 id="client">Client</h2>
<h3 id="install">Install</h3>
<pre><code>sudo apt install nfs-common
</code></pre>
<h3 id="create-a-directory">Create a directory</h3>
<pre><code>sudo mkdir -p /mnt/nfs_share_desktop
sudo chown $USER: /mnt/nfs_share_desktop/
</code></pre>
<h3 id="mount">Mount</h3>
<pre><code>sudo mount desktop.local:/home/ben/ /mnt/nfs_share_desktop/
</code></pre>
<p>or via <code>/etc/fstab</code>:</p>
<pre><code>desktop.local:/home /mnt/nfs_share_desktop nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
</code></pre>
<p>finally:</p>
<pre><code>sudo mount -a
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[symfonycasts course notes - JavaScript for PHP Geeks: ES6/ES2015 (New JavaScript)]]></title>
<link href="/blog/2021/05/07/ES6"/>
<updated>2021-05-07T00:00:00+00:00</updated>
<id>/blog/2021/05/07/ES6</id>
<content type="html"><![CDATA[<h2 id="1---ecmascript-2015-%2F-es6-%2F-harmony-%2F-cookies">1 - ECMAScript 2015 / ES6 / Harmony / Cookies</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/ecmascript-es6-harmony</p>
<p>JavaScript is not a language like PHP that has one core code. In reality, JavaScript is nothing more than a standard. When a new version of JavaScript is released, it simply means that the core group has said:</p>
<pre><code>Here are some functions and language changes that we think would make JavaScript more hipster. Now, quick, everyone go and implement these!
</code></pre>
<p>And guess what? The language isn't even called JavaScript! It's called ECMAScript. And there is a group of smart people that work on new versions of ECMAScript. But unlike PHP, that doesn't mean they're writing code: they're simply deciding what should be included in the next version. Then, it's up to each browser and JavaScript engine to implement that. But as we will learn later... some smart people in the JS world have found a way around needing to wait for browser support...</p>
<h2 id="2---arrow-functions">2 - Arrow Functions</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/arrow-functions#play</p>
<p>You will see the first big feature or ES2015 used everywhere... and at first, it looks weird. Very simply, there is a new, shorter syntax for creating anonymous functions.</p>
<p>In ES2015, we can remove the word function, and add an "equal arrow" (=>) after the arguments:</p>
<pre><code>$.ajax({
url: Routing.generate('rep_log_list'),
}).then(function(data) {
$.each(data.items, function(key, repLog) {
self._addRow(repLog);
});
})
</code></pre>
<p>equivalent:</p>
<pre><code> $.ajax({
url: Routing.generate('rep_log_list'),
}).then((data) => {
$.each(data.items, function(key, repLog) {
self._addRow(repLog);
});
})
</code></pre>
<h3 id="%C2%A0the-arrow-function%27s-secret-superpower-this"> The Arrow Function's (Secret) Superpower (this)</h3>
<p>It turns out, a classic anonymous function and the new arrow function do have one difference: when you use an arrow function, the this variable is preserved. That's awesome news, and it's why I now use the arrow function everywhere in my code.</p>
<p>We can finally remove this silly var = self thing. And instead, below, use this</p>
<h3 id="arrow-functions-everywhere%21">Arrow Functions Everywhere!</h3>
<p>Here, we're not using this, but I like to stay consistent and use the arrow function everywhere.</p>
<h3 id="arrow-function-without-a-body">Arrow Function without a Body</h3>
<p>In this case, the arrow function is nothing more than a single return statement. In this situation, to be extra fancy, you can remove the function body and return statement entirely:</p>
<pre><code> swal({
preConfirm: () => {
return this._deleteRepLog($link);
}
</code></pre>
<p>equivalent to:</p>
<pre><code>swal({
preConfirm: () => this._deleteRepLog($link)
});
</code></pre>
<p>When you don't have the curly braces, it means that this value will be returned. It looks weird at first, but it means the same thing that we had before. You will see this kind of stuff in code examples.</p>
<h2 id="4---var-versus-let%3A-scope%21">4 - var Versus let: Scope!</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/var-let-scope#play</p>
<p>And this is the difference between var and let. With var - just like with any variable in PHP - a variable's scope is the function it's inside of, plus any embedded functions.</p>
<p>But let is different: it's said to be "block-scoped". That means that anytime you have a new open curly brace ({) - like an if statement or for loop - you've entered a new scope for let. In this case, let is equal to 42, only inside of the if statement. Outside, it's a completely different variable, which is set to 10.</p>
<h2 id="5---var-versus-let%3A-hoisting%21">5 - var Versus let: Hoisting!</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/var-let-hoisting#play</p>
<p>When you use var to initialize a variable, when JavaScript executes, it basically finds all of your var variables, goes to the top of that variable's scope - usually the top of whatever function it's inside of, but in this case, it's the top of the file - and effectively does this: var aGreatNumber. That initializes the variable, but doesn't set it to any value. This is called variable hoisting: and it's the reason that we get undefined instead of an error when we try to use a variable that's declared with var... before it's declared.</p>
<p>But when we change this to let, we already saw that this does throw a ReferenceError. And that's kinda great! I mean, isn't that what we would expect to happen when we reference a variable that hasn't been created yet!</p>
<h2 id="6---const-versus-let">6 - const Versus let</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/const#play</p>
<p>As far as scope goes, const and let work the same. So really, const and let are identical... except that you can't modify a const variable.</p>
<p>Here's the truth: when you use const, it's not that the value of that variable can't change. The object can change. Instead, using const means that you cannot reassign the aGreatObject variable to something else in memory. It must be assigned only once, to this object. But after that, the object is free to change.</p>
<h2 id="7---object-literals-%26-optional-args">7 - Object Literals & Optional Args</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/object-literals-optional-args</p>
<h3 id="object-keys...-without-the-key">Object Keys... without the Key</h3>
<p>Well, in ES2015, if your key and your value are the same, you can just leave off the key:</p>
<pre><code> $.ajax({
url: url,
method: 'POST',
data: JSON.stringify(data)
})
</code></pre>
<p>is equivalent to :</p>
<pre><code> $.ajax({
url,
method: 'POST',
data: JSON.stringify(data)
})
</code></pre>
<h3 id="short-method-syntax">Short Method Syntax</h3>
<pre><code>loadRepLogs: function() {
},
</code></pre>
<p>is equivalent to :</p>
<pre><code> loadRepLogs() {
},
</code></pre>
<h3 id="optional-args">Optional Args</h3>
<pre><code>getTotalWeightString(maxWeight = 500)
</code></pre>
<h2 id="8---legit-javascript-classes">8 - Legit JavaScript Classes</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/class-syntax#play</p>
<pre><code>class Helper {
constructor($wrapper) {
this.$wrapper = $wrapper;
}
calculateTotalWeight() {
let totalWeight = 0;
this.$wrapper.find('tbody tr').each((index, element) => {
totalWeight += $(element).data('weight');
});
return totalWeight;
}
}
</code></pre>
<h2 id="%C2%A09---static-class-methods"> 9 - Static Class Methods</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/static-class-methods#play</p>
<p>declare a static method:</p>
<pre><code> static _calculateWeights($elements) {
}
</code></pre>
<p>call it referencing the class name:</p>
<pre><code>Helper._calculateWeights(this.$wrapper.find('tbody tr'));
</code></pre>
<h2 id="10---class-inheritance-and-super-calls">10 - Class Inheritance and super Calls</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/extends-super-calls#play</p>
<pre><code>class AGreatClass {
constructor(greatNumber) {
this.greatNumber = greatNumber;
}
returnGreatThings() {
return this.greatNumber;
}
}
class AnotherGreatClass extends AGreatClass{
constructor(greatNumber, greatWord) {
super(greatNumber); //call parent constructor
this.greatWord = greatWord;
}
returnGreatThings() {
let greatNumber = super.returnGreatThings();
return [greatNumber, this.greatWord];
}
}
const aGreatObject = new AnotherGreatClass(42, 'adventure');
console.log(
aGreatObject.returnGreatThings()
);
</code></pre>
<h2 id="11---destructuring">11 - Destructuring</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/destructuring</p>
<pre><code> let {id, itemLabel, reps} = repLog;
</code></pre>
<p>Yep, this weird line is actually going to create three new variables - id, itemLabel, and reps - set to the values of the id, itemLabel and reps keys in repLog.</p>
<h2 id="12---the...-spread-operator">12 - The... Spread Operator</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/spread-operator#play</p>
<pre><code>let printThreeThings = function(thing1, thing2, thing3) {
console.log(thing1, thing2, thing3);
};
let yummyThings = ['pizza', 'gelato', 'sushi'];
printThreeThings(...yummyThings)
</code></pre>
<h3 id="%C2%A0spread-operator-as-an-array-merge"> Spread Operator as an Array Merge</h3>
<pre><code>let yummyThings = ['pizza', 'gelato', 'sushi', 'cheeseburger'];
let greatThings = ['swimming', 'sunsets', ...yummyThings, 'New Orleans'];
</code></pre>
<h3 id="spread-operator-for-creating-a-new-array">Spread Operator for Creating a new Array</h3>
<pre><code>let yummyThings = ['pizza', 'gelato', 'sushi', 'cheeseburger'];
let greatThings = ['swimming', 'sunsets', ...yummyThings, 'New Orleans'];
let copyOfGreatThings = greatThings;
copyOfGreatThings.push('summer');
console.log(greatThings);
console.log(copyOfGreatThings);
</code></pre>
<p>Here's the question: we know summer now lives in copyOfGreatThings(). But does it also now live inside of greatThings? Try it! It does! Summer lives in both arrays! And this makes sense: arrays are objects in JavaScript, and just like in PHP, objects are passed by reference. In reality, greatThings and copyOfGreatThings are identical: they both point to the same array in memory.</p>
<pre><code>let yummyThings = ['pizza', 'gelato', 'sushi', 'cheeseburger'];
let greatThings = ['swimming', 'sunsets', ...yummyThings, 'New Orleans'];
let copyOfGreatThings = [...greatThings];
</code></pre>
<p>And that is it! This will create a new array, and then put each item from greatThings into it, one-by-one.</p>
<p>Try it! Yes! We can see summer in the copy, but we did not modify the original array.</p>
<h2 id="13---template-strings">13 - Template Strings</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/template-strings#play</p>
<p>This is the way we used to do things in JavaScript. But no more! Thanks to ES2015, we now have something called Template Strings. And it's awesome. Instead of quotes, use a tick. And as soon as you do that, you're allowed to put variables inside of your string! Remove this single quote plus garbage. Replace it with ${, the variable name, then }</p>
<pre><code>const favoriteFood = 'gelato';
const iLoveFood = `The year is ${(new Date()).getFullYear()} and my favorite food is ${favoriteFood}`;
console.log(iLoveFood);
</code></pre>
<h3 id="%C2%A0turning-our-template-into-a-function"> Turning our Template into a Function</h3>
<pre><code> const rowTemplate = (repLog) => `
<tr data-weight="${repLog.totalWeightLifted}">
<td>${repLog.itemLabel}</td>
<td>${repLog.reps}</td>
<td>${repLog.totalWeightLifted}</td>
<td>
<a href="#"
class="js-delete-rep-log"
data-url="${repLog.links._self}"
>
<span class="fa fa-trash"></span>
</a>
</td>
</tr>
`;
const html = rowTemplate(repLog);
</code></pre>
<h2 id="14---the-for-of-loop">14 - The for of Loop</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/for-of-loop#play</p>
<pre><code>for (let repLog of data.items) {
this._addRow(repLog);
}
</code></pre>
<h2 id="15---map-and-weakmap">15 - Map and WeakMap</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/map-weak-map</p>
<pre><code>let foods = new Map();
foods.set('italian', 'gelato');
foods.set('mexican', 'tortas');
foods.set('canadian', 'poutine');
let southernUsStates = ['Tennessee', 'Kentucky', 'Texas'];
foods.set(southernUsStates, 'hot chicken');
console.log(
foods.get('italian'),
foods.get(southernUsStates),
foods.size
);
</code></pre>
<h2 id="16---private-variables-%26-weakmap">16 - Private Variables & WeakMap</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/private-vars-weak-map#play</p>
<p>For us it means we should use Map in normal situations... and WeakMap only if we find ourselves with this problem.</p>
<h2 id="17---array%2C-set-and-es2016">17 - Array, Set and ES2016</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/array-setf</p>
<pre><code>let foods = []; //same as new Array();
foods.push('gelato');
foods.push('tortas');
foods.push('gelato');
console.log(foods);
</code></pre>
<h3 id="introducing-set">Introducing Set</h3>
<pre><code>let foods = new Set();
foods.add('gelato');
foods.add('tortas');
foods.add('gelato');
console.log(foods);
</code></pre>
<p>That's the key difference between Array and Set: Set should be used when you need a unique collection of items. It automatically makes sure that duplicates aren't added.</p>
<h2 id="18----yarn-%26-npm%3A-installing-babel">18 - yarn & npm: Installing Babel</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/npm-yarn#play</p>
<p>In a nut shell, Babel reads new JavaScript code, i.e. ES2015 code, and recompiles it to old JavaScript so that all browsers can understand it. Yea, it literally reads source code and converts it to different source code. It's wild!</p>
<p>yarn add babel-cli --dev</p>
<h2 id="19---babel%3A-transpiling-to-old-javascript">19 - Babel: Transpiling to Old JavaScript</h2>
<p>https://symfonycasts.com/screencast/javascript-es6/babel#play</p>
<pre><code>yarn add babel-preset-env --dev
</code></pre>
<p>.babelrc:</p>
<pre><code>{
"presets": ["env"]
}
</code></pre>
<h3 id="%C2%A0babel-and-the-polyfill"> babel-and-the-polyfill</h3>
<p>abel's job is to convert all the new language constructs and syntaxes to the old version. But if there are new objects or functions, it leaves those. Instead, you should use something called a polyfill. Specifically, babel-polyfill. This is another JavaScript library that adds missing functionality, like WeakMap, if it doesn't exist in whatever browser is running our code.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[symfonycasts course notes - JavaScript for PHP Geeks]]></title>
<link href="/blog/2021/03/24/sfcast-javascript"/>
<updated>2021-03-24T00:00:00+00:00</updated>
<id>/blog/2021/03/24/sfcast-javascript</id>
<content type="html"><![CDATA[<h1 id="about">About</h1>
<p>This is raw notes from the great symfony cast course "JavaScript for PHP Geeks".
Content have been fully copied/pasted from the official <a href="https://symfonycasts.com/screencast/javascript">public resource</a>.</p>
<h1 id="notes">Notes</h1>
<h2 id="1---lift-stuff%21-the-js--prefix">1 - Lift Stuff! The js- Prefix</h2>
<p>https://symfonycasts.com/screencast/javascript/js-class-prefix
Our goal is the second, and by prefixing the class with js-, it makes that crystal clear. This is a fairly popular standard: when you add a class for JavaScript, give it a js- prefix so that future you doesn't need to wonder which classes are for styling and which are for JavaScript. Future you will... thank you.</p>
<p>https://symfonycasts.com/screencast/javascript/js-class-prefix</p>
<pre><code><a href="#" class="js-delete-rep-log">
<span class="fa fa-trash"></span>
</a>
<script>
$('.js-delete-rep-log').on('click', function() {
console.log('todo delete!');
});
</script>
</code></pre>
<h2 id="2---document.ready-%26-ordering">2 - (document).ready() & Ordering</h2>
<p>https://symfonycasts.com/screencast/javascript/document-ready-ordering</p>
<p>put js at the bottom surrounded by a <code>$(document).ready(function()</code>:</p>
<pre><code>
<script>
$(document).ready(function() {
$('.js-delete-rep-log').on('click', function () {
console.log('todo delete!');
});
});
</script>
</code></pre>
<h2 id="3---all-about-event-bubbling">3 - All about Event Bubbling</h2>
<p>https://symfonycasts.com/screencast/javascript/event-bubbling#play</p>
<p>So the $ in $table isn't doing anything special, but it is a fairly common convention to denote a variable that is a jQuery object.</p>
<pre><code>
<script>
$(document).ready(function() {
var $table = $('.js-rep-log-table');
$table.find('.js-delete-rep-log').on('click', function () {
console.log('todo delete!');
});
$table.find('tbody tr').on('click', function() {
console.log('row clicked!');
});
});
</script>
</code></pre>
<h2 id="4---the-event-argument-%26-stoppropagation">4 - The Event Argument & stopPropagation</h2>
<p>https://symfonycasts.com/screencast/javascript/event-propagation</p>
<p>It turns out that when you return false from a listener function, it is equivalent to calling e.preventDefault() and e.stopPropagation(). To prove it, remove the return false and refresh:</p>
<pre><code>
<script>
$(document).ready(function() {
var $table = $('.js-rep-log-table');
$table.find('.js-delete-rep-log').on('click', function (e) {
e.preventDefault();
console.log('todo delete!');
});
$table.find('tbody tr').on('click', function() {
console.log('row clicked!');
});
});
</script>
</code></pre>
<p>e.preventDefault() versus e.stopPropagation()</p>
<p>The e.preventDefault() says: don't do the default, browser behavior for this event. Normally, when you "click" a "link", your browser navigates to its href... which is a #. So cool, e.preventDefault() stops that! But e.stopPropagation() tells your browser to not bubble this event any further up the DOM tree. And that's probably not what you want. Do you really want your event listener to be so bold that it decides to prevent all other listeners from firing? I've literally never had a use-case for this.</p>
<pre><code>
<script>
$(document).ready(function() {
var $table = $('.js-rep-log-table');
$table.find('.js-delete-rep-log').on('click', function (e) {
e.preventDefault();
console.log('todo delete!');
});
$table.find('tbody tr').on('click', function() {
console.log('row clicked!');
});
});
</script>
</code></pre>
<h2 id="5---the-dom-element-object">5 - The DOM Element Object</h2>
<p>https://symfonycasts.com/screencast/javascript/dom-element-object#play</p>
<p>Using e.target</p>
<p>Because there's another way to find out which element was clicked... a better way, and it involves our magical e event argument. Just say $(e.target). target is a property on the event object that points to the actual element that was clicked. Then, .addClass('text-danger'):</p>
<p>Actually, no... our browser is kinda lying to us: e.target is a DOM Element object. Google for that and find the W3Schools page all about it. You see, every element on the page is represented by a JavaScript object, a DOM Element object.</p>
<pre><code>$(e.target).addClass('text-danger');
</code></pre>
<p><=>
e.target.className = e.target.className+' text-danger';</p>
<h2 id="6---the-magical-this-variable-%26-currenttarget">6 - The Magical this Variable & currentTarget</h2>
<p>https://symfonycasts.com/screencast/javascript/this-current-target#play</p>
<p>this is equivalent to e.currentTarget, the DOM Element that we originally attached our listener to.
Ultimately that means that we can say, $(this).addClass('text-danger'):
So, use the this variable, it's your friend. But realize what's going on: this is just a shortcut to e.currentTarget. That fact is going to become critically important in just a little while.</p>
<pre><code>
<script>
$(document).ready(function() {
var $table = $('.js-rep-log-table');
$table.find('.js-delete-rep-log').on('click', function (e) {
e.preventDefault();
$(this).addClass('text-danger');
$(this).find('.fa')
.removeClass('fa-trash')
.addClass('fa-spinner')
.addClass('fa-spin');
});
$table.find('tbody tr').on('click', function() {
console.log('row clicked!');
});
});
</script>
</code></pre>
<h2 id="7---a-great-place-to-hide-things%21-the-data--attributes">7 - A Great Place to Hide Things! The data- Attributes</h2>
<p>https://symfonycasts.com/screencast/javascript/data-attribute</p>
<p>Adding a data-url Attribute</p>
<p>This is a really common problem, and the solution is to somehow attach extra metadata to our DOM about the RepLog, so we can read it in JavaScript. And guess what! There's an official, standard, proper way to do this! It's via a data attribute. Yep, according to those silly "rules" of the web, you're not really supposed to invent new attributes for your elements. Well, unless the attribute starts with data-, followed by lowercase letters. That's totally allowed!</p>
<pre><code>
<script>
$(document).ready(function() {
var $table = $('.js-rep-log-table');
$table.find('.js-delete-rep-log').on('click', function (e) {
e.preventDefault();
$(this).addClass('text-danger');
$(this).find('.fa')
.removeClass('fa-trash')
.addClass('fa-spinner')
.addClass('fa-spin');
var deleteUrl = $(this).data('url');
var $row = $(this).closest('tr');
var $totalWeightContainer = $table.find('.js-total-weight');
var newWeight = $totalWeightContainer.html() - $row.data('weight');
$.ajax({
url: deleteUrl,
method: 'DELETE',
success: function() {
$row.fadeOut();
$totalWeightContainer.html(newWeight);
}
});
});
$table.find('tbody tr').on('click', function() {
console.log('row clicked!');
});
});
</script>
</code></pre>
<h2 id="8---organizing-with-objects%21">8 - Organizing with Objects!</h2>
<p>https://symfonycasts.com/screencast/javascript/javascript-objects</p>
<p>Don't Call your Handler Function: Pass It</p>
<p>There's one teenie detail I want you to notice: when we specify the event callback, this.handleRepLogDelete - we're not executing it:
I mean, there are no () on the end of it. Nope, we're simply passing the function as a reference to the on() function. If you forget and add (), things will get crazy.</p>
<pre><code>
<script>
var RepLogApp = {
initialize: function($wrapper) {
this.$wrapper = $wrapper;
this.$wrapper.find('.js-delete-rep-log').on(
'click',
this.handleRepLogDelete
);
this.$wrapper.find('tbody tr').on(
'click',
this.handleRowClick
);
},
handleRepLogDelete: function(e) {
e.preventDefault();
$(this).addClass('text-danger');
$(this).find('.fa')
.removeClass('fa-trash')
.addClass('fa-spinner')
.addClass('fa-spin');
var deleteUrl = $(this).data('url');
var $row = $(this).closest('tr');
var $totalWeightContainer = this.$wrapper.find('.js-total-weight');
var newWeight = $totalWeightContainer.html() - $row.data('weight');
$.ajax({
url: deleteUrl,
method: 'DELETE',
success: function() {
$row.fadeOut();
$totalWeightContainer.html(newWeight);
}
});
},
handleRowClick: function() {
console.log('row clicked!');
}
};
$(document).ready(function() {
var $table = $('.js-rep-log-table');
RepLogApp.initialize($table);
});
</script>
</code></pre>
<h2 id="%C2%A09---%22static%22-objects-%26-the-this-variable"> 9 - "Static" Objects & the this Variable</h2>
<p>https://symfonycasts.com/screencast/javascript/static-objects-this#play</p>
<p>When this is not this</p>
<p>Here's the deal: whenever you are in a callback function, like the success callback of an AJAX call, the callback of an event listener, or even when passing a callback to the setTimeout() function, the this variable in your callback changes to be something else. And we already knew that! We know that this in our event handler is actually a reference to the DOM Element object that was clicked. So the this variable in handleRepLogDelete is not our RepLogApp object, even though we're inside of that object. Creepy!</p>
<p>We're going to talk a lot more about this situation... in a moment.
Referencing your Object "Statically"</p>
<p>Fortunately, for now, the fix is easy. If you think about it, the RepLogApp object is very similar to a class in PHP that has only static properties and methods. I mean, could we create multiple RepLogApp objects? Nope! There can only ever be one. And because of that, each property - like $wrapper - acts like a static property: you set and access it, but it's attached to our "static", single object: RepLogApp, not to an individual instance of RepLogApp.</p>
<pre><code>
<script>
var RepLogApp = {
initialize: function($wrapper) {
this.$wrapper = $wrapper;
this.$wrapper.find('.js-delete-rep-log').on(
'click',
this.handleRepLogDelete
);
this.$wrapper.find('tbody tr').on(
'click',
this.handleRowClick
);
},
updateTotalWeightLifted: function() {
var totalWeight = 0;
this.$wrapper.find('tbody tr').each(function() {
totalWeight += $(this).data('weight');
});
this.$wrapper.find('.js-total-weight').html(totalWeight);
},
handleRepLogDelete: function(e) {
e.preventDefault();
$(this).addClass('text-danger');
$(this).find('.fa')
.removeClass('fa-trash')
.addClass('fa-spinner')
.addClass('fa-spin');
var deleteUrl = $(this).data('url');
var $row = $(this).closest('tr');
$.ajax({
url: deleteUrl,
method: 'DELETE',
success: function() {
$row.fadeOut('normal', function() {
$row.remove();
RepLogApp.updateTotalWeightLifted();
});
}
});
},
handleRowClick: function() {
console.log('row clicked!');
}
};
$(document).ready(function() {
var $table = $('.js-rep-log-table');
RepLogApp.initialize($table);
});
</script>
</code></pre>
<h2 id="%C2%A010---getting-to-the-bottom-of-the-this-variable"> 10 - Getting to the bottom of the this Variable</h2>
<p>https://symfonycasts.com/screencast/javascript/understanding-this-magic#play</p>
<p>How do I Know what this Is?</p>
<p>Here's the deal: when you call a function in JavaScript, you can choose to change what this is inside of that function when you call it. That means you could have one function and 10 different people could call your function and decide to set this to 10 different things.</p>
<p>Now, in reality, it's not that bad. But we do need to remember one rule of thumb: whenever you have a callback function - meaning someone else is calling a function after something happens - this will have changed. We've already seen this a lot: in the click functions, inside of .each(), inside of success and even inside of $row.fadeOut():</p>
<pre><code>
<script>
var RepLogApp = {
initialize: function($wrapper) {
this.$wrapper = $wrapper;
this.$wrapper.find('.js-delete-rep-log').on(
'click',
this.handleRepLogDelete
);
this.$wrapper.find('tbody tr').on(
'click',
this.handleRowClick
);
var newThis = {cat: 'meow', dog: 'woof'};
this.whatIsThis.call(newThis, 'hello');
},
whatIsThis: function(greeting) {
console.log(this, greeting);
},
updateTotalWeightLifted: function() {
var totalWeight = 0;
this.$wrapper.find('tbody tr').each(function() {
totalWeight += $(this).data('weight');
});
this.$wrapper.find('.js-total-weight').html(totalWeight);
},
handleRepLogDelete: function(e) {
e.preventDefault();
$(this).addClass('text-danger');
$(this).find('.fa')
.removeClass('fa-trash')
.addClass('fa-spinner')
.addClass('fa-spin');
var deleteUrl = $(this).data('url');
var $row = $(this).closest('tr');
$.ajax({
url: deleteUrl,
method: 'DELETE',
success: function() {
$row.fadeOut('normal', function() {
$(this).remove();
RepLogApp.updateTotalWeightLifted();
});
}
});
},
handleRowClick: function() {
console.log('row clicked!');
}
};
$(document).ready(function() {
var $table = $('.js-rep-log-table');
RepLogApp.initialize($table);
});
</script>
</code></pre>
<h2 id="11---fixing-%22this%22-with-bind">11 - Fixing "this" with bind()</h2>
<p>https://symfonycasts.com/screencast/javascript/fixing-this-bind#play</p>
<p>But wow, that's a lot of work, and it'll be a bit ugly! Instead, there's a simpler way. First, realize that whenever you have an anonymous function, you could refactor it into an individual method on your object. If we did that, then I would recommend binding that function so that this is the RepLogApp object inside.</p>
<p>But if that feels like overkill and you want to keep using anonymous functions, then simply go above the callback and add var self = this</p>
<p>Use bind() to make sure that this is always this inside any methods in your object.
Make sure to reference your object with this, instead of your object's name. This isn't an absolute rule, but unless you know what you're doing, this will give you more flexibility in the long-run.</p>
<pre><code>
<script>
var RepLogApp = {
initialize: function($wrapper) {
this.$wrapper = $wrapper;
this.$wrapper.find('.js-delete-rep-log').on(
'click',
this.handleRepLogDelete.bind(this)
);
this.$wrapper.find('tbody tr').on(
'click',
this.handleRowClick.bind(this)
);
},
updateTotalWeightLifted: function() {
var totalWeight = 0;
this.$wrapper.find('tbody tr').each(function() {
totalWeight += $(this).data('weight');
});
this.$wrapper.find('.js-total-weight').html(totalWeight);
},
handleRepLogDelete: function(e) {
e.preventDefault();
var $link = $(e.currentTarget);
$link.addClass('text-danger');
$link.find('.fa')
.removeClass('fa-trash')
.addClass('fa-spinner')
.addClass('fa-spin');
var deleteUrl = $link.data('url');
var $row = $link.closest('tr');
var self = this;
$.ajax({
url: deleteUrl,
method: 'DELETE',
success: function() {
$row.fadeOut('normal', function() {
$(this).remove();
self.updateTotalWeightLifted();
});
}
});
},
handleRowClick: function() {
console.log('row clicked!');
}
};
$(document).ready(function() {
var $table = $('.js-rep-log-table');
RepLogApp.initialize($table);
});
</script>
</code></pre>
<h2 id="%C2%A012---immediately-invoked-function-expression%21"> 12 - Immediately Invoked Function Expression!</h2>
<p>https://symfonycasts.com/screencast/javascript/immediately-invoked-function-expression</p>
<p>There are two things to check out. First, all we're doing is creating a function: it starts on top, and ends at the bottom with the }. But by adding the (), we are immediately executing that function. We're creating a function and then calling it!</p>
<p>Why on earth would we do this? Because! Variable scope in JavaScript is function based. When you create a variable with var, it's only accessible from inside of the function where you created it. If you have functions inside of that function, they have access to it too, but ultimately, that function is its home.</p>
<p>Before, when we weren't inside of any function, our two variables effectively became global: we could access them from anywhere. But now that we're inside of a function, the RepLogApp and Helper variables are only accessible from inside of this self-executing function.</p>
<p>This means that when we refresh, we get Helper is not defined. We just made the Helper variable private!</p>
<h2 id="13---the-window-object-%26-global-variables">13 - The window Object & Global Variables</h2>
<p>https://symfonycasts.com/screencast/javascript/window-global-vars#play</p>
<p>Inside of our self-executing function, we - of course - also have access to any global variables, like window or the $ jQuery variable. But, instead of relying on these global variables, you'll often see people pass those variables into the function. It's a little weird, so let's see it.</p>
<pre><code>(function(window, $) {
//...
})(window, jQuery);
</code></pre>
<p>Forget var? It goes Global!</p>
<p>To tell JavaScript to stop being such a pushover, at the top of the RepLogApp.js file, inside quotes, say 'use strict':</p>
<pre><code>'use strict';
</code></pre>
<h2 id="15---the-object-prototype%21">15 - The Object prototype!</h2>
<p>https://symfonycasts.com/screencast/javascript/object-prototype</p>