Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,100 --> 00:00:04,910
Now we learned how to listen to events.
2
00:00:04,910 --> 00:00:06,840
Now here's one important thing.
3
00:00:06,840 --> 00:00:11,170
Currently, I'm updating the counter here in my HTML code.
4
00:00:11,170 --> 00:00:15,100
In the Vue control parts, but in the HTML code
5
00:00:15,100 --> 00:00:19,380
and it's generally is considered a bad
6
00:00:19,380 --> 00:00:23,130
or not optimal practice to do that.
7
00:00:23,130 --> 00:00:28,070
You don't wanna put too much logic into your HTML code.
8
00:00:28,070 --> 00:00:30,270
Instead, the HTML code should really
9
00:00:30,270 --> 00:00:32,480
just be about outputting stuff.
10
00:00:33,720 --> 00:00:36,580
Logic like this, even though it's not too complex,
11
00:00:36,580 --> 00:00:39,770
should typically go into your JavaScript code.
12
00:00:39,770 --> 00:00:44,360
Now how could we get this logic into our JavaScript code?
13
00:00:45,490 --> 00:00:48,370
Well, we wanna do something on every click.
14
00:00:48,370 --> 00:00:49,890
What do you typically use
15
00:00:49,890 --> 00:00:53,730
if you wanna do something at a certain point of time,
16
00:00:53,730 --> 00:00:56,430
potentially in a repeated way?
17
00:00:56,430 --> 00:00:58,690
Well, you need a function that can be called.
18
00:00:58,690 --> 00:00:59,630
You need a function,
19
00:00:59,630 --> 00:01:01,920
which you can call when the Add button is clicked
20
00:01:01,920 --> 00:01:03,610
and you need a function which you can call
21
00:01:03,610 --> 00:01:05,310
when the Reduce button is clicked.
22
00:01:06,420 --> 00:01:11,420
Now of course, we could specify a function here, add,
23
00:01:11,520 --> 00:01:15,010
and there, try to set counter equal to counter plus one.
24
00:01:15,010 --> 00:01:16,390
But for multiple reasons,
25
00:01:16,390 --> 00:01:17,690
this wouldn't work
26
00:01:17,690 --> 00:01:21,320
for one counter is not a globally available variable,
27
00:01:21,320 --> 00:01:23,220
it is a data property,
28
00:01:23,220 --> 00:01:25,400
hence it's not available here.
29
00:01:25,400 --> 00:01:27,560
In addition, even if it would be available
30
00:01:27,560 --> 00:01:28,950
for whatever reason,
31
00:01:28,950 --> 00:01:33,350
add now is a globally created function.
32
00:01:33,350 --> 00:01:36,680
But in your Vue controlled HTML code,
33
00:01:36,680 --> 00:01:38,780
you can't use such a function.
34
00:01:38,780 --> 00:01:43,330
You can only use in this Vue controlled HTML part,
35
00:01:43,330 --> 00:01:46,290
what is defined in your Vue app.
36
00:01:46,290 --> 00:01:48,550
And that's why we use methods for that,
37
00:01:48,550 --> 00:01:51,280
and we did already learn about methods before.
38
00:01:51,280 --> 00:01:55,000
Now, we don't just use methods to manually call them
39
00:01:55,000 --> 00:01:58,840
as we did before, we can also use methods to connect them,
40
00:01:58,840 --> 00:02:02,140
to event listeners and let Vue call them for us
41
00:02:02,140 --> 00:02:04,260
when a certain event occurs.
42
00:02:04,260 --> 00:02:07,180
For example, here, we can add a add method,
43
00:02:07,180 --> 00:02:10,060
though this name here is totally up to you,
44
00:02:10,060 --> 00:02:13,180
you could also name it add counter or increment
45
00:02:13,180 --> 00:02:15,700
or whatever you want, but I'll name it add.
46
00:02:15,700 --> 00:02:20,030
And in here, you can now update this counter,
47
00:02:20,030 --> 00:02:24,100
just important as you learned with this counter.
48
00:02:24,100 --> 00:02:25,530
In your JavaScript code,
49
00:02:25,530 --> 00:02:29,510
you refer to your data properties with this.
50
00:02:29,510 --> 00:02:31,510
This counter plus plus,
51
00:02:31,510 --> 00:02:34,240
or the longer form this counter
52
00:02:34,240 --> 00:02:37,760
equals this counter plus one, it's the same.
53
00:02:37,760 --> 00:02:41,390
And with that, we have a method which we can call
54
00:02:41,390 --> 00:02:43,440
to update our counter.
55
00:02:43,440 --> 00:02:45,430
Now this method doesn't return anything
56
00:02:45,430 --> 00:02:47,120
because it doesn't have to.
57
00:02:47,120 --> 00:02:50,040
We'll not use the method like we did before
58
00:02:50,040 --> 00:02:52,960
to dynamically output something here.
59
00:02:52,960 --> 00:02:57,330
Instead, we're using it to run it when a click occurs
60
00:02:57,330 --> 00:02:59,570
and therefore, this does need to return anything
61
00:02:59,570 --> 00:03:03,370
because the click listener doesn't want a return value.
62
00:03:03,370 --> 00:03:07,350
So now here, we can call add like this
63
00:03:07,350 --> 00:03:10,450
and if I now saved it and reload, I can click add
64
00:03:10,450 --> 00:03:14,010
and it still works because we're executing the same code
65
00:03:14,010 --> 00:03:16,040
just in a different way.
66
00:03:16,040 --> 00:03:18,990
And this is actually the batter way of doing it,
67
00:03:18,990 --> 00:03:21,200
putting your logic into JavaScript
68
00:03:21,200 --> 00:03:23,640
and just pointing at that logic.
69
00:03:23,640 --> 00:03:26,550
That is generally considered better,
70
00:03:26,550 --> 00:03:29,720
even though technically, it here works in the same way.
71
00:03:29,720 --> 00:03:33,170
But consider cases where you have more complex logic
72
00:03:33,170 --> 00:03:36,110
and it quickly makes sense that you should put that logic
73
00:03:36,110 --> 00:03:39,350
into JavaScript and not into HTML.
74
00:03:39,350 --> 00:03:42,220
So it's not bad to get started with that as a habit,
75
00:03:42,220 --> 00:03:43,410
right from the start.
76
00:03:45,100 --> 00:03:48,230
Now besides calling add like this, by the way,
77
00:03:48,230 --> 00:03:50,660
you can also just point at it
78
00:03:50,660 --> 00:03:53,930
so that you tell Vue, "Hey, that's the name of the function
79
00:03:53,927 --> 00:03:56,337
"or of the method to be precise.
80
00:03:56,337 --> 00:03:58,937
"You should execute when a click occurs."
81
00:03:58,940 --> 00:04:03,530
Vue accepts both, you explicitly calling it like this,
82
00:04:03,530 --> 00:04:05,460
or you pointing at it,
83
00:04:05,460 --> 00:04:07,330
Vue detects what you're doing,
84
00:04:07,330 --> 00:04:09,920
and it will do the right thing in both cases.
85
00:04:10,820 --> 00:04:13,220
Now typically, you just wanna point at it
86
00:04:13,220 --> 00:04:16,200
since that is closer to vanilla JavaScript
87
00:04:16,200 --> 00:04:19,030
and how you would set up an event listener there
88
00:04:19,030 --> 00:04:23,110
but it is worth noting that both syntaxes are allowed
89
00:04:23,110 --> 00:04:24,860
as you can see here.
90
00:04:24,860 --> 00:04:28,510
Well, and now let's do the same for reducing our counter.
91
00:04:29,370 --> 00:04:30,760
Of course, try this on your own.
92
00:04:30,760 --> 00:04:32,650
Here's a quick pause for you to do that
93
00:04:32,650 --> 00:04:34,550
and there after, we'll do it together.
94
00:04:37,380 --> 00:04:38,910
Were you successful?
95
00:04:38,910 --> 00:04:41,870
Well, again, it's pretty much the same code.
96
00:04:41,870 --> 00:04:45,530
We can add a reduce method,
97
00:04:45,530 --> 00:04:47,610
again, this name is up to you
98
00:04:47,610 --> 00:04:49,470
and in there, we just set this counter
99
00:04:49,470 --> 00:04:53,940
equal to this counter minus one, or we use the shorter form,
100
00:04:53,940 --> 00:04:56,080
which looks like this, which is also okay.
101
00:04:58,500 --> 00:05:01,730
And now in here on this second button,
102
00:05:01,730 --> 00:05:06,020
we can call reduce like this, or as I just mentioned,
103
00:05:06,020 --> 00:05:08,570
just point at it, both is accepted.
104
00:05:08,570 --> 00:05:11,020
And now we've got our working buttons
105
00:05:11,020 --> 00:05:12,800
with one important change,
106
00:05:12,800 --> 00:05:16,820
now the logic is outsourced into JavaScript,
107
00:05:16,820 --> 00:05:19,160
which is where your logic should be
108
00:05:19,160 --> 00:05:22,460
and the HTML code is cleaner
109
00:05:22,460 --> 00:05:23,750
and this is better.
110
00:05:23,750 --> 00:05:25,400
This is considered to be better,
111
00:05:25,400 --> 00:05:28,860
so it is a good idea to get started with this approach
112
00:05:28,860 --> 00:05:30,450
and with this pattern.
113
00:05:32,320 --> 00:05:35,380
Now that is how we can use methods.
114
00:05:35,380 --> 00:05:40,340
We cannot just use them in curly braces or with the bind
115
00:05:40,340 --> 00:05:44,200
to dynamically derive a value we wanna output
116
00:05:44,200 --> 00:05:47,360
but we can also use them with event listeners
117
00:05:47,360 --> 00:05:49,740
to define codes that should be executed
118
00:05:49,740 --> 00:05:52,010
when the event occurs.
119
00:05:52,010 --> 00:05:55,530
And in reality, you will more often use methods
120
00:05:55,530 --> 00:05:57,160
to connect them to events,
121
00:05:57,160 --> 00:05:58,420
than you will use them
122
00:05:58,420 --> 00:06:01,030
to output something dynamically derived.
123
00:06:01,030 --> 00:06:04,360
I just showed this before to show you that it's possible
124
00:06:04,360 --> 00:06:06,280
and to introduce methods
125
00:06:06,280 --> 00:06:08,660
but typically, you'll use them like this
126
00:06:08,660 --> 00:06:10,540
in combination with event listeners.
9407
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.