Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,540 --> 00:00:06,810
In the last video, I mentioned that the way that we structured our code is not ideal.
2
00:00:07,200 --> 00:00:10,890
In this video, I'm going to explain exactly what I mean by that.
3
00:00:11,460 --> 00:00:12,270
So let's.
4
00:00:12,270 --> 00:00:13,380
What do I mean by that?
5
00:00:13,740 --> 00:00:19,500
Well, in order to understand, let's actually look at this dev and all of the HTML elements inside
6
00:00:19,500 --> 00:00:19,950
of it.
7
00:00:20,250 --> 00:00:23,160
So the dev with the option container.
8
00:00:23,550 --> 00:00:25,530
So we have this dev right over here.
9
00:00:25,860 --> 00:00:31,860
And then inside of this dev, we have this H4 that has the title of the option.
10
00:00:32,220 --> 00:00:38,010
And then right over here, we also have this dev that is going to house all of the buttons and then
11
00:00:38,010 --> 00:00:40,470
we have all of the button options.
12
00:00:40,680 --> 00:00:43,590
All of them have relatively the same class.
13
00:00:44,070 --> 00:00:47,490
Now, if we go over here, let's go all the way to the bottom.
14
00:00:47,730 --> 00:00:51,210
You can see that we actually have another option container.
15
00:00:51,390 --> 00:00:54,690
And similarly, the HTML structure is exactly the same.
16
00:00:54,930 --> 00:01:02,550
We have an H4 and then we also have this button's option div and then we have buttons inside of it.
17
00:01:02,760 --> 00:01:08,190
Now the number of buttons might be different, but we still have buttons with the same structure nonetheless.
18
00:01:08,520 --> 00:01:11,010
And the same thing applies over here.
19
00:01:11,340 --> 00:01:15,840
So we have the dev, we have the H1, we have the container, we have the buttons.
20
00:01:16,260 --> 00:01:23,370
So you can see here that we are reusing this HTML template multiple times throughout our application.
21
00:01:23,850 --> 00:01:27,510
Now this is causing a lot of code duplication.
22
00:01:27,720 --> 00:01:31,800
And when we have something like this, what is this a good candidate for?
23
00:01:32,190 --> 00:01:39,030
Well, it's a good candidate to put it into a component and then just pass the necessary data that it
24
00:01:39,030 --> 00:01:40,980
needs via props.
25
00:01:41,340 --> 00:01:42,630
So what do I mean by that?
26
00:01:42,810 --> 00:01:47,850
Well, why don't we actually just place this over here, this whole dev inside of a component and just
27
00:01:47,850 --> 00:01:49,500
pass a data that it needs?
28
00:01:49,740 --> 00:01:50,850
What data does it need?
29
00:01:51,060 --> 00:01:56,070
Well, it needs to have the title, and it also needs to have the button data.
30
00:01:56,790 --> 00:01:59,730
So let's actually take a look at a quick diagram to understand this.
31
00:02:00,120 --> 00:02:05,010
So right now, we have a bunch we have three different option containers.
32
00:02:05,280 --> 00:02:09,870
So we can just have three different components and then we can just iterate over them.
33
00:02:09,870 --> 00:02:15,660
And for each component, we can just give it a title that is going to be choose gender, and then we
34
00:02:15,660 --> 00:02:20,480
can give it an array of the different buttons and then we can just give each button value.
35
00:02:20,490 --> 00:02:23,520
So gendered all boy gender unisex gender dog girl.
36
00:02:23,820 --> 00:02:25,620
Now over here we can do the exact same thing.
37
00:02:25,620 --> 00:02:30,900
I didn't put it for space reasons, but over here we can also have the choose, the length, and then
38
00:02:30,900 --> 00:02:33,240
we can have all of the different lengths.
39
00:02:33,600 --> 00:02:36,480
And that's really all it is that we need to pass.
40
00:02:36,780 --> 00:02:42,060
Another thing that we also will need to pass is the option state, and we already have that here, so
41
00:02:42,060 --> 00:02:45,390
we can just go ahead and pass that in relatively easily.
42
00:02:45,900 --> 00:02:51,750
So let's actually go ahead and create a component for this and surpassing in the data that is necessary.
43
00:02:52,020 --> 00:02:54,900
And that's really going to clean up our code right now.
44
00:02:54,900 --> 00:03:01,110
Let's take a look at how long our code is around to all five lines of code for this app view file.
45
00:03:01,440 --> 00:03:07,020
And let's just see exactly how much shorter it gets once we finish writing this component.
46
00:03:07,770 --> 00:03:12,300
So I'll just keep a quick note here that it was 205 so let's just write a comment.
47
00:03:12,690 --> 00:03:15,210
205 my sure.
48
00:03:15,210 --> 00:03:20,850
If that's a comment, maybe we can put the comment here to all five lines of code.
49
00:03:21,510 --> 00:03:21,950
Okay.
50
00:03:21,960 --> 00:03:23,700
So let's just go ahead and do this right now.
51
00:03:24,180 --> 00:03:29,430
Now the way that we build components and Nux is a little bit differently.
52
00:03:29,880 --> 00:03:35,160
So what we're going to do here is we're going to create a directory called Components.
53
00:03:35,160 --> 00:03:36,120
I already created this.
54
00:03:36,120 --> 00:03:38,490
I'm actually delete it and just do it again with you guys.
55
00:03:39,030 --> 00:03:43,110
So what we're going to do here is we're going to create a directory, not in the node modules, in the
56
00:03:43,110 --> 00:03:50,070
root directory, we're going to create a directory called components and it has to be called components
57
00:03:50,070 --> 00:03:56,720
because any file that lives in here, any view file that lives in here, Nux is going to detect that
58
00:03:56,730 --> 00:04:03,120
as a component and it's going to give it a special ability that we usually don't get with just plain
59
00:04:03,120 --> 00:04:04,080
old view.
60
00:04:04,890 --> 00:04:13,350
So right over here I am going to create a component, so I'm going to create an option dot view file.
61
00:04:13,800 --> 00:04:16,320
And then right over here I'm going to define a template.
62
00:04:17,280 --> 00:04:21,690
And then what I'm going to do is I'm just going to grab one of the option containers.
63
00:04:22,020 --> 00:04:26,370
So I'm going to go over here and let's just grab the options container.
64
00:04:26,370 --> 00:04:29,430
Let's just grab one of these option containers.
65
00:04:29,610 --> 00:04:30,810
Zoom over here.
66
00:04:30,990 --> 00:04:31,710
Grab that.
67
00:04:32,840 --> 00:04:40,390
And let's paste that here and now let's start working on surpassing in some of this data.
68
00:04:40,400 --> 00:04:42,590
So we need to pass in some data as props.
69
00:04:43,100 --> 00:04:49,430
So what I'm going to do actually inside of the app view, I'm going to create an array with all of the
70
00:04:49,430 --> 00:04:52,640
different option option data.
71
00:04:53,060 --> 00:05:01,190
So over here, I'm going to go on the create options array and then over here these are going to be
72
00:05:01,190 --> 00:05:02,800
all of the different options.
73
00:05:02,810 --> 00:05:05,240
So the first option is we're going to have an object here.
74
00:05:05,630 --> 00:05:07,250
It's going to have a title of.
75
00:05:08,520 --> 00:05:10,860
Choose a gender.
76
00:05:12,150 --> 00:05:19,080
And then we're going to specify all of the different buttons over here.
77
00:05:20,850 --> 00:05:22,500
So we're going to say gender.
78
00:05:22,530 --> 00:05:23,790
Dot girl.
79
00:05:24,360 --> 00:05:25,830
They're going to say gender.
80
00:05:25,860 --> 00:05:27,630
Dot unisex.
81
00:05:28,860 --> 00:05:30,720
We're also going to say gender.
82
00:05:31,260 --> 00:05:32,640
Dot boy.
83
00:05:32,970 --> 00:05:39,480
Now, one other thing that we also have to pass in is exactly what category this falls in, because
84
00:05:39,480 --> 00:05:46,410
the button eventually is going to update the option dot gender or option dot popularity or option dot
85
00:05:46,410 --> 00:05:46,830
length.
86
00:05:47,100 --> 00:05:51,230
So have to specify what category does this option fallen.
87
00:05:51,240 --> 00:05:55,800
So I'm going to say over here that this falls in category.
88
00:05:58,370 --> 00:05:59,120
Gender.
89
00:06:00,480 --> 00:06:02,220
So the gender category.
90
00:06:02,520 --> 00:06:03,960
And that's really all it is that we need.
91
00:06:04,260 --> 00:06:08,760
So now let's go over here and let's add now.
92
00:06:10,180 --> 00:06:12,370
So this is going to be the popularity.
93
00:06:12,370 --> 00:06:13,420
So choose.
94
00:06:14,300 --> 00:06:17,690
The name's Popularity.
95
00:06:19,340 --> 00:06:21,500
So popularity.
96
00:06:22,800 --> 00:06:26,160
And then over here, this is going to update the popularity.
97
00:06:26,520 --> 00:06:29,430
And then over here, we're going to have our array of buttons.
98
00:06:29,760 --> 00:06:35,130
So obviously this is going to be popularity dot, let's say trendy.
99
00:06:35,130 --> 00:06:36,840
And then over here we only have two buttons.
100
00:06:36,840 --> 00:06:40,350
So it's going to be popularity dot unique.
101
00:06:41,040 --> 00:06:43,080
Let's save that one more left.
102
00:06:43,590 --> 00:06:45,240
Let's just copy what we have here.
103
00:06:46,510 --> 00:06:47,530
Put that in there.
104
00:06:48,190 --> 00:06:50,710
And then right over here, we're going to say three.
105
00:06:51,960 --> 00:06:56,130
And we are going to say choose the names length.
106
00:06:56,820 --> 00:06:59,820
So choose names length.
107
00:07:00,660 --> 00:07:03,450
And this is going to be length.
108
00:07:03,720 --> 00:07:07,650
That's the category that we want to update or remember, we want to update this category when we click
109
00:07:07,650 --> 00:07:08,730
on a specific button.
110
00:07:09,510 --> 00:07:15,870
And then over here we're going to say length dot, let's do short.
111
00:07:16,870 --> 00:07:20,470
And over here we're going to do length diet.
112
00:07:22,500 --> 00:07:24,750
So length thought all.
113
00:07:25,290 --> 00:07:26,010
And then over here.
114
00:07:26,040 --> 00:07:26,390
Length.
115
00:07:26,400 --> 00:07:26,940
Thought.
116
00:07:27,300 --> 00:07:27,990
Long.
117
00:07:29,200 --> 00:07:30,310
Length dot.
118
00:07:32,610 --> 00:07:33,400
Well, it's gender.
119
00:07:33,420 --> 00:07:34,800
Obviously, we want length here.
120
00:07:36,120 --> 00:07:37,770
I was wondering why that wasn't showing up.
121
00:07:37,890 --> 00:07:40,380
Okay, so now we have our array.
122
00:07:40,620 --> 00:07:48,840
So now what I'm going to do is I'm going to remove all of the different option containers that we have.
123
00:07:49,200 --> 00:07:54,510
So I'm going to actually I'm going to go over here to the options container to scroll all the way down
124
00:07:54,510 --> 00:07:58,980
to this div and then all the other divs up until the options container.
125
00:07:58,980 --> 00:08:01,290
I'm just going to delete all the HTML elements.
126
00:08:01,860 --> 00:08:04,920
So over here, let's just delete all that.
127
00:08:05,490 --> 00:08:06,960
And what I'm going to do now.
128
00:08:08,540 --> 00:08:10,010
I am just going to.
129
00:08:11,910 --> 00:08:14,910
Display the option component.
130
00:08:15,420 --> 00:08:19,290
Now, you might be wondering exactly how are we going to display the option components?
131
00:08:19,290 --> 00:08:20,460
Do we need to import it?
132
00:08:20,460 --> 00:08:21,690
Do we need to define it?
133
00:08:22,050 --> 00:08:26,550
No, actually, all we need to do is just display it like this.
134
00:08:26,910 --> 00:08:32,070
And this right here is going to have to have the same exact name as we called this right over here in
135
00:08:32,070 --> 00:08:32,940
the view file.
136
00:08:33,360 --> 00:08:40,050
So what actually ends up happening is because we put our component in the components directory, nux
137
00:08:40,050 --> 00:08:43,110
is smart enough to auto import it in.
138
00:08:43,590 --> 00:08:46,650
So if this wasn't called components we wouldn't have this ability.
139
00:08:46,650 --> 00:08:52,170
But we can just auto imported in without having any manual imports ourselves.
140
00:08:52,710 --> 00:08:54,390
So we're going to go ahead and do that.
141
00:08:54,570 --> 00:09:00,510
And then what we're going to do is we're going to have av4, so we're going to iterate over the options
142
00:09:00,510 --> 00:09:01,080
array.
143
00:09:01,620 --> 00:09:03,450
So for each option.
144
00:09:04,620 --> 00:09:08,610
So option in options.
145
00:09:08,940 --> 00:09:10,980
And we're going to need to supply a key.
146
00:09:11,400 --> 00:09:14,310
So let's just say option dot title.
147
00:09:14,310 --> 00:09:15,570
That's always going to be unique.
148
00:09:15,810 --> 00:09:18,600
And that's exactly how we are going to display it.
149
00:09:18,870 --> 00:09:28,380
And right away, you can see that we went from 205 lines of code to 153 lines of code.
150
00:09:28,770 --> 00:09:35,970
That's because now we just removed a huge block of our HTML that was relatively unnecessary.
151
00:09:36,000 --> 00:09:37,980
We should have definitely had it somewhere else.
152
00:09:38,490 --> 00:09:43,680
Now, of course, if we go to our Web page and I do a quick refresh, you can see it's not going to
153
00:09:43,680 --> 00:09:44,130
work.
154
00:09:44,400 --> 00:09:48,750
And that's because now what we need to do is start passing in some props.
155
00:09:49,050 --> 00:09:57,960
And once we pass in the props, then we can go ahead and then we can go ahead and start populating this
156
00:09:57,960 --> 00:09:59,460
data accordingly.
157
00:09:59,790 --> 00:10:02,730
So let's actually go ahead and do that in the next video.
14399
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.