Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,160 --> 00:00:04,860
Now that we got the basics about events down,
2
00:00:04,860 --> 00:00:07,160
let's talk about event modifiers,
3
00:00:07,160 --> 00:00:10,020
which is another useful feature built into Vue.
4
00:00:10,960 --> 00:00:13,030
Here's an example for an event modifier
5
00:00:13,030 --> 00:00:15,280
that can sometimes be useful.
6
00:00:15,280 --> 00:00:18,780
Let's say you had a form HTML element,
7
00:00:18,780 --> 00:00:23,780
in there you had some input and you had a button
8
00:00:25,550 --> 00:00:27,780
that says, sign up, whatever,
9
00:00:27,780 --> 00:00:29,410
if you reload, you have that here.
10
00:00:29,410 --> 00:00:31,650
Now the problem you'll have by default,
11
00:00:31,650 --> 00:00:33,270
if you leave it like that,
12
00:00:33,270 --> 00:00:36,410
even if it's in the Vue controlled HTML part
13
00:00:36,410 --> 00:00:39,850
is that when you click this button, this page will reload.
14
00:00:41,100 --> 00:00:44,650
And the reason for that is that the browser default
15
00:00:44,650 --> 00:00:48,630
for a form with a button like this is to submit that form
16
00:00:48,630 --> 00:00:53,630
and send an HTTP request to the server serving this app.
17
00:00:53,750 --> 00:00:55,720
In this case we have no such server,
18
00:00:55,720 --> 00:00:57,110
we're running this locally,
19
00:00:57,110 --> 00:00:59,800
but still the browser simply sends a request
20
00:00:59,800 --> 00:01:02,730
to our local machine therefore, you could say.
21
00:01:02,730 --> 00:01:05,530
Now, of course, we have no code in place
22
00:01:05,530 --> 00:01:07,710
to handle this request.
23
00:01:07,710 --> 00:01:11,150
Now we'll get there later in this course,
24
00:01:11,150 --> 00:01:14,290
but typically with frameworks like Vue,
25
00:01:14,290 --> 00:01:17,510
you wanna prevent this browser default
26
00:01:17,510 --> 00:01:21,620
and instead you wanna handle this manually
27
00:01:21,620 --> 00:01:25,620
in JavaScript with help of Vue in your Vue app
28
00:01:25,620 --> 00:01:29,280
and you wanna read the user input there, validate it
29
00:01:29,280 --> 00:01:32,120
and then send it manually to a backend server
30
00:01:32,120 --> 00:01:34,440
and store it in a database, for example.
31
00:01:34,440 --> 00:01:37,010
So you wanna prevent that browser default
32
00:01:37,010 --> 00:01:38,890
of automatically sending it.
33
00:01:40,100 --> 00:01:42,120
Now, there are two ways of doing that.
34
00:01:42,120 --> 00:01:45,620
One of them is cool and the other one is well more obvious,
35
00:01:45,620 --> 00:01:46,810
I would say.
36
00:01:46,810 --> 00:01:49,690
You can listen to the submit event here,
37
00:01:49,690 --> 00:01:52,250
which again is a default browser event
38
00:01:52,250 --> 00:01:55,630
to which you can listen on form elements.
39
00:01:55,630 --> 00:01:57,380
Then we could have a method here,
40
00:01:57,380 --> 00:02:00,270
submit form like this
41
00:02:00,270 --> 00:02:03,900
at which we can point here, submit form.
42
00:02:03,900 --> 00:02:06,870
Now we're telling Vue that whenever the form is submitted,
43
00:02:06,870 --> 00:02:09,510
submit form should be executed.
44
00:02:09,510 --> 00:02:11,270
And here we could do whatever you want.
45
00:02:11,270 --> 00:02:13,760
For example, showing alert submitted.
46
00:02:14,800 --> 00:02:17,640
Now right now, the problem is that we still have
47
00:02:17,640 --> 00:02:18,470
the browser default.
48
00:02:18,473 --> 00:02:20,693
If I click this, I get the alert,
49
00:02:20,690 --> 00:02:24,130
but the page reloads thereafter, and when the page reloads,
50
00:02:24,130 --> 00:02:26,800
that means we lose all our data, right?
51
00:02:26,800 --> 00:02:30,320
If I had a counter here and I click sign up,
52
00:02:30,320 --> 00:02:32,070
you'll see that all the data is lost
53
00:02:32,070 --> 00:02:35,630
because of the page reload, the Vue app restarts
54
00:02:35,630 --> 00:02:36,630
so to say.
55
00:02:36,630 --> 00:02:39,440
So we haven't solved the problem,
56
00:02:39,440 --> 00:02:43,330
but remember that we get this default event object,
57
00:02:43,330 --> 00:02:46,060
I talked about that in the last lecture.
58
00:02:46,060 --> 00:02:49,320
Now we can call prevent default on that.
59
00:02:49,320 --> 00:02:52,190
This is a default browser mechanism,
60
00:02:52,190 --> 00:02:53,880
it's built in JavaScript,
61
00:02:53,880 --> 00:02:56,810
which is totally independent from Vue.
62
00:02:56,810 --> 00:03:00,130
Even if we just used Vanilla JavaScript
63
00:03:00,130 --> 00:03:02,640
on that default event object, which we get,
64
00:03:02,640 --> 00:03:04,660
we could call prevent default.
65
00:03:05,545 --> 00:03:07,215
And this would tell the browser that the form
66
00:03:07,210 --> 00:03:08,890
should not be submitted,
67
00:03:08,890 --> 00:03:11,380
that we don't want that browser default.
68
00:03:12,220 --> 00:03:15,340
So now if I reload, I can change my counter,
69
00:03:15,340 --> 00:03:18,990
click sign up, we see the alert and the counter still
70
00:03:18,990 --> 00:03:21,740
is there because now that browser default
71
00:03:21,740 --> 00:03:23,070
has been prevented.
72
00:03:24,190 --> 00:03:27,330
This works and there's nothing wrong with it,
73
00:03:27,330 --> 00:03:30,920
but Vue has a nicer way for us.
74
00:03:30,920 --> 00:03:32,690
We can remove all that code
75
00:03:32,690 --> 00:03:37,010
and instead use a so-called event modifier.
76
00:03:37,010 --> 00:03:40,160
There are certain built in modifiers,
77
00:03:40,160 --> 00:03:44,630
which you can connect to events you're listening to,
78
00:03:44,633 --> 00:03:46,743
let's say to change that event behavior,
79
00:03:46,740 --> 00:03:51,740
and you add such a modifier with a dot after the event name
80
00:03:52,160 --> 00:03:53,810
after v-on.
81
00:03:53,810 --> 00:03:57,710
So v-on: then the event name and then a dot
82
00:03:57,710 --> 00:03:59,910
if you want the modifier.
83
00:03:59,910 --> 00:04:03,110
Now, there are only a couple of supported modifiers.
84
00:04:03,110 --> 00:04:05,840
You can find a full list in the official docs.
85
00:04:05,840 --> 00:04:10,350
For example, there is a prevent modifier and this modifier,
86
00:04:10,350 --> 00:04:14,680
which is built into Vue will prevent the browser default.
87
00:04:14,680 --> 00:04:19,210
So it will basically do what we did manually a second ago
88
00:04:19,210 --> 00:04:20,940
just with less code.
89
00:04:21,820 --> 00:04:25,830
There always is a stop modifier for stopping propagation,
90
00:04:25,830 --> 00:04:28,850
but here we need to prevent modifier.
91
00:04:28,850 --> 00:04:32,490
And with data, if you reload, I can sign up.
92
00:04:32,490 --> 00:04:35,600
We see that alert and still the page doesn't reload,
93
00:04:35,600 --> 00:04:38,240
thanks to this modifier.
94
00:04:38,240 --> 00:04:42,440
So that's nice to know, and that will come in handy later.
95
00:04:42,440 --> 00:04:44,770
We don't just have such event modifiers
96
00:04:44,770 --> 00:04:47,590
to change the event and browser behavior,
97
00:04:47,590 --> 00:04:51,340
we also have modifiers we can connect to events
98
00:04:51,340 --> 00:04:54,460
to change when the event occurs.
99
00:04:54,460 --> 00:04:56,790
And one example would be our buttons.
100
00:04:56,790 --> 00:04:59,440
Let's say for whatever reason,
101
00:04:59,440 --> 00:05:03,480
we only wanna subtract five if the user right clicks
102
00:05:03,480 --> 00:05:04,800
on the button.
103
00:05:04,800 --> 00:05:08,670
The default of course is to react to a left click.
104
00:05:08,670 --> 00:05:10,750
But if you want it to react to a right click,
105
00:05:10,750 --> 00:05:13,480
we could add a modifier here as well.
106
00:05:13,480 --> 00:05:15,510
And we can set it to right.
107
00:05:16,400 --> 00:05:20,110
Alternative values are left, which you don't need to specify
108
00:05:20,110 --> 00:05:24,470
because it's the default, middle and well, right.
109
00:05:24,470 --> 00:05:26,830
And this tells Vue that here,
110
00:05:26,830 --> 00:05:30,220
you only wanna react to a right click.
111
00:05:30,220 --> 00:05:32,700
So when you're using a mouse event like click,
112
00:05:32,700 --> 00:05:36,230
you can use the right, left and middle event modifiers
113
00:05:36,230 --> 00:05:40,220
to change which mouse button triggers this event.
114
00:05:41,210 --> 00:05:42,710
So if we now save this, you see,
115
00:05:42,710 --> 00:05:45,660
I can add 10 by clicking with the left key
116
00:05:45,660 --> 00:05:47,520
but if I click on subtract five
117
00:05:47,520 --> 00:05:49,740
with the left key, nothing happens.
118
00:05:49,740 --> 00:05:52,150
If I use the right mouse button, though,
119
00:05:52,150 --> 00:05:53,800
which you can tell I'm doing,
120
00:05:53,800 --> 00:05:56,100
because my context menu opens up,
121
00:05:56,100 --> 00:05:58,610
now the counter gets smaller.
122
00:05:58,610 --> 00:06:01,920
Of course we can argue whether that's useful here,
123
00:06:01,920 --> 00:06:06,220
but it's definitely useful to be aware of those modifiers.
124
00:06:06,220 --> 00:06:08,860
And there's a last kind of modifiers.
125
00:06:08,860 --> 00:06:11,000
And that would be key modifiers
126
00:06:11,000 --> 00:06:13,610
when you're listening to keyboard events
127
00:06:13,610 --> 00:06:15,890
like here on the input element.
128
00:06:15,890 --> 00:06:19,440
Currently we're updating the name upon the input event here,
129
00:06:19,440 --> 00:06:21,370
so on every keystroke.
130
00:06:21,370 --> 00:06:26,180
Now let's say we wanna update the internally stored name
131
00:06:26,180 --> 00:06:29,950
sold as name property with every keystroke,
132
00:06:29,950 --> 00:06:34,480
but the name that's being output here should only be updated
133
00:06:34,480 --> 00:06:39,480
when the user hits enter on this input element.
134
00:06:40,710 --> 00:06:43,800
Well, for that, we could have another data property
135
00:06:43,800 --> 00:06:47,540
confirmed name, which is the actual name
136
00:06:47,540 --> 00:06:49,420
we wanna output here.
137
00:06:49,420 --> 00:06:54,420
So here we got the confirmed name in this paragraph
138
00:06:55,570 --> 00:06:59,960
and the confirmed name, unlike the internally stored name,
139
00:06:59,960 --> 00:07:01,810
which is just named name,
140
00:07:01,810 --> 00:07:04,930
the confirmed name should only update here
141
00:07:04,930 --> 00:07:08,280
when the user hits the enter button in this input field.
142
00:07:09,140 --> 00:07:13,190
For that, we can add another event listener
143
00:07:13,190 --> 00:07:14,720
on the input element.
144
00:07:14,720 --> 00:07:16,600
Yes, you can have more than one.
145
00:07:16,600 --> 00:07:20,370
So here we are listening to input with v-on input.
146
00:07:20,370 --> 00:07:24,040
Now I'll add another v-on directive and also listen
147
00:07:24,040 --> 00:07:27,140
to the key up event,
148
00:07:27,140 --> 00:07:31,080
which is triggered when a key is pressed and released again.
149
00:07:31,080 --> 00:07:35,480
And here, I wanna trigger a method which I have yet to add,
150
00:07:35,480 --> 00:07:39,730
which I'll call confirm input or confirm name,
151
00:07:39,730 --> 00:07:41,520
whatever you want.
152
00:07:41,520 --> 00:07:44,160
The goal here in confirm input
153
00:07:44,160 --> 00:07:47,960
is to set this confirmed name,
154
00:07:47,960 --> 00:07:50,640
so this new data property we added,
155
00:07:50,640 --> 00:07:52,920
which we're outputting in this paragraph,
156
00:07:52,920 --> 00:07:57,450
set this confirmed name equal to the internally stored name,
157
00:07:57,450 --> 00:07:59,630
which we update with every keystroke.
158
00:08:01,640 --> 00:08:06,640
So here v-on key up, I wanna point at confirm input.
159
00:08:08,270 --> 00:08:10,910
Now currently this would fire whenever
160
00:08:10,910 --> 00:08:13,790
any key is pressed and released.
161
00:08:13,790 --> 00:08:17,480
So currently, if I reload here, we would basically have
162
00:08:17,480 --> 00:08:21,440
the same behavior as before with every keystroke
163
00:08:21,440 --> 00:08:25,220
the input event but then also the key up event is triggered
164
00:08:25,220 --> 00:08:27,440
and therefore we basically confirm the input
165
00:08:27,440 --> 00:08:28,590
with every keystroke.
166
00:08:29,690 --> 00:08:33,620
So that's probably not the final behavior we want.
167
00:08:33,620 --> 00:08:37,260
Instead, we can use another modifier with a dot here
168
00:08:37,260 --> 00:08:40,750
to tell Vue that since this is a keyboard event,
169
00:08:40,750 --> 00:08:42,410
we only wanna react,
170
00:08:42,410 --> 00:08:46,990
we only wanna trigger this event upon certain special keys
171
00:08:46,990 --> 00:08:49,160
that are pressed and released.
172
00:08:49,160 --> 00:08:51,620
For example, the enter key,
173
00:08:51,620 --> 00:08:56,070
you could also listen to control or shift or page down,
174
00:08:56,070 --> 00:08:58,720
all keyboard keys are possible here,
175
00:08:58,720 --> 00:09:01,160
but I wanna listen to the enter key.
176
00:09:02,010 --> 00:09:05,140
And with that, I'm saying, hey, when the enter key
177
00:09:05,140 --> 00:09:07,040
is pressed and released,
178
00:09:07,040 --> 00:09:09,920
then confirm input should be executed.
179
00:09:10,850 --> 00:09:13,570
So now if I save that and reload,
180
00:09:13,570 --> 00:09:18,570
I can enter Maximilian here and we see nothing in that
181
00:09:18,770 --> 00:09:23,770
green box, but if I placed my cursor in here and hit enter,
182
00:09:24,010 --> 00:09:25,890
now we see that output here.
183
00:09:27,460 --> 00:09:30,370
So these are event modifiers.
184
00:09:30,370 --> 00:09:33,570
Now I'm quickly going to auto format this HTML code
185
00:09:33,570 --> 00:09:36,940
with the format document key combination here
186
00:09:36,940 --> 00:09:39,460
in Visual Studio Code to make this more readable,
187
00:09:39,460 --> 00:09:40,770
but it's the same code as before
188
00:09:40,770 --> 00:09:42,530
just structured differently.
189
00:09:42,530 --> 00:09:44,560
So that's the input we just worked on,
190
00:09:44,560 --> 00:09:46,880
we have the input event and the key up event
191
00:09:46,880 --> 00:09:49,890
with the enter modifier to change when
192
00:09:49,890 --> 00:09:51,760
confirm input executes.
14529
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.