1
00:00:02,540 --> 00:00:04,220
All right, let's get this thing started by

2
00:00:04,220 --> 00:00:08,780
building out a module using named exports.

3
00:00:08,780 --> 00:00:10,280
Now, if you go into our demo code that we

4
00:00:10,280 --> 00:00:12,780
have before you, look at app.js. We've got

5
00:00:12,780 --> 00:00:14,410
a whole bunch of code in here, and the

6
00:00:14,410 --> 00:00:16,470
whole point of what we're trying to do is

7
00:00:16,470 --> 00:00:18,330
we're going to start pulling this out into

8
00:00:18,330 --> 00:00:20,235
modules, and the first thing we're going

9
00:00:20,235 --> 00:00:22,860
to pull out is this template that we have.

10
00:00:22,860 --> 00:00:25,160
So there's a function starting right here

11
00:00:25,160 --> 00:00:27,820
on line 13 called sessionTemplate. We're

12
00:00:27,820 --> 00:00:30,958
just going to grab that whole thing and

13
00:00:30,958 --> 00:00:33,800
we're going to cut that. We're going to

14
00:00:33,800 --> 00:00:40,340
create a new file called template.js.

15
00:00:40,340 --> 00:00:44,160
Let's just paste that in there. All right,

16
00:00:44,160 --> 00:00:46,260
so now we have a chunk of code in a file

17
00:00:46,260 --> 00:00:49,500
called template ready to go, except

18
00:00:49,500 --> 00:00:51,880
nothing can get to it, right? So the whole

19
00:00:51,880 --> 00:00:53,560
point of this is we want to expose some

20
00:00:53,560 --> 00:00:56,520
things, and what we're going to do just so

21
00:00:56,520 --> 00:00:58,310
I can work through what all the different

22
00:00:58,310 --> 00:01:00,380
styles of named exports are is we're going

23
00:01:00,380 --> 00:01:02,150
to break this up a little bit, we'll clean

24
00:01:02,150 --> 00:01:06,180
up this function some. So let's come up

25
00:01:06,180 --> 00:01:08,350
here and we're going to do, we're going to

26
00:01:08,350 --> 00:01:10,300
create an error message and we're going to

27
00:01:10,300 --> 00:01:17,400
take this error and stick it up there. All

28
00:01:17,400 --> 00:01:22,980
right. And then if there's an error, we're

29
00:01:22,980 --> 00:01:25,750
going to return an error message. Now, we

30
00:01:25,750 --> 00:01:28,060
also have this function here, and I'm not

31
00:01:28,060 --> 00:01:30,090
a huge proponent of having anonymous

32
00:01:30,090 --> 00:01:33,190
functions inside my map, so let's just cut

33
00:01:33,190 --> 00:01:39,540
that out and let's say, hey, sessions.map

34
00:01:39,540 --> 00:01:43,740
applyTemplate. And then right here on line

35
00:01:43,740 --> 00:01:49,950
3 we'll paste that function and give it a

36
00:01:49,950 --> 00:01:51,930
name of applyTemplate. Clean everything

37
00:01:51,930 --> 00:02:00,820
up. Now, I have three items in this

38
00:02:00,820 --> 00:02:02,220
JavaScript file, I've got my error

39
00:02:02,220 --> 00:02:04,190
message, I've got my function,

40
00:02:04,190 --> 00:02:05,700
applyTemplate, and I've got my

41
00:02:05,700 --> 00:02:10,030
sessionTemplate. And really, all I want is

42
00:02:10,030 --> 00:02:15,570
for the consuming application to know what

43
00:02:15,570 --> 00:02:19,080
sessionTemplate is. And I don't want it to

44
00:02:19,080 --> 00:02:20,460
know about anything else, I just want to

45
00:02:20,460 --> 00:02:22,220
know about that, and the way we do that

46
00:02:22,220 --> 00:02:25,352
using export is I can come in here,

47
00:02:25,352 --> 00:02:26,470
there's a couple different ways, but the

48
00:02:26,470 --> 00:02:30,440
first way we're going to talk about is I

49
00:02:30,440 --> 00:02:33,740
just say export function sessionTemplate.

50
00:02:33,740 --> 00:02:35,600
Now I'll give you a little sneak peek.

51
00:02:35,600 --> 00:02:38,080
We'll talk about this next time in the

52
00:02:38,080 --> 00:02:42,640
next module, but this gets exported,

53
00:02:42,640 --> 00:02:44,440
sessionTemplate gets exported, and then I

54
00:02:44,440 --> 00:02:46,500
just import it on the other side, I just

55
00:02:46,500 --> 00:02:49,850
do import sessionTemplate. And notice the

56
00:02:49,850 --> 00:02:51,900
curly braces there, that's how this is

57
00:02:51,900 --> 00:02:53,910
going to be exposed. And actually I can

58
00:02:53,910 --> 00:02:58,000
come up, if we just copy this, I can

59
00:02:58,000 --> 00:03:00,910
actually just export my error message and

60
00:03:00,910 --> 00:03:02,790
I can export my applyTemplate, I can

61
00:03:02,790 --> 00:03:06,050
export everything that I want and just

62
00:03:06,050 --> 00:03:09,590
export it all. And that's fine. Then I

63
00:03:09,590 --> 00:03:11,690
could just import all those things in a

64
00:03:11,690 --> 00:03:15,330
list. That's one way to do it, and that's

65
00:03:15,330 --> 00:03:17,140
fine. You can go through and you can look

66
00:03:17,140 --> 00:03:18,680
through this JavaScript file and see

67
00:03:18,680 --> 00:03:22,680
everything that's being exported. There's

68
00:03:22,680 --> 00:03:25,680
another way to go about this, and it's the

69
00:03:25,680 --> 00:03:30,630
way that I prefer, which is to bundle all

70
00:03:30,630 --> 00:03:31,880
that up. And so we'll come down here at

71
00:03:31,880 --> 00:03:34,440
the bottom and we're going to say

72
00:03:34,440 --> 00:03:39,080
export, and we're going to make a list. So

73
00:03:39,080 --> 00:03:44,000
we're going to say sessionTemplate, and

74
00:03:44,000 --> 00:03:45,830
let's just export sessionTemplate and

75
00:03:45,830 --> 00:03:47,480
errorMessage, that's all we need to

76
00:03:47,480 --> 00:03:49,170
export. We don't need to export anything

77
00:03:49,170 --> 00:03:51,970
else. We don't need export this

78
00:03:51,970 --> 00:03:53,240
applyTemplate because this is just a

79
00:03:53,240 --> 00:03:55,520
private function that only sessionTemplate

80
00:03:55,520 --> 00:04:00,690
uses. So let's save that. Now I have a

81
00:04:00,690 --> 00:04:03,720
module called template that's going to

82
00:04:03,720 --> 00:04:06,200
work just fine, and the only thing

83
00:04:06,200 --> 00:04:08,900
available to anything consuming this

84
00:04:08,900 --> 00:04:11,620
module is the errorMessage and the

85
00:04:11,620 --> 00:04:14,610
sessionTemplate, and that's all. All

86
00:04:14,610 --> 00:04:16,750
right, let's do one more thing. Let's go

87
00:04:16,750 --> 00:04:19,380
back over to app and we've got this

88
00:04:19,380 --> 00:04:23,310
getSessions thing. So let's apply the same

89
00:04:23,310 --> 00:04:26,750
premise to that. So if I right-click on

90
00:04:26,750 --> 00:04:30,330
JS, let's create a new file called

91
00:04:30,330 --> 00:04:36,670
sessionRepository.js. So let's do

92
00:04:36,670 --> 00:04:39,940
getSessions, let's cut that, just that one

93
00:04:39,940 --> 00:04:41,790
piece. And so we're going to create a

94
00:04:41,790 --> 00:04:46,100
little repository that's going to handle

95
00:04:46,100 --> 00:04:48,315
the getting of sessions. And we're going

96
00:04:48,315 --> 00:04:49,670
to do the same thing we just did on the

97
00:04:49,670 --> 00:04:51,530
other side. So I'm going to come down here

98
00:04:51,530 --> 00:04:55,060
and we're going to create a a sessionURL.

99
00:04:55,060 --> 00:04:57,840
This is where the sessions come from.

100
00:04:57,840 --> 00:05:02,240
We'll just cut that out there. All right,

101
00:05:02,240 --> 00:05:04,410
now that we have that, let's use this

102
00:05:04,410 --> 00:05:08,950
sessionURL right here, and then we're

103
00:05:08,950 --> 00:05:16,790
going to store the sessionList. Excellent.

104
00:05:16,790 --> 00:05:19,860
So now, instead of right here when

105
00:05:19,860 --> 00:05:21,150
everything comes back, instead of just

106
00:05:21,150 --> 00:05:24,610
resolving and sending the target response,

107
00:05:24,610 --> 00:05:30,370
let's just save that sessionList just like

108
00:05:30,370 --> 00:05:32,750
that. All right, now I have the

109
00:05:32,750 --> 00:05:34,790
sessionList that's available, I've got

110
00:05:34,790 --> 00:05:37,450
getSessions, which goes out and and pulls

111
00:05:37,450 --> 00:05:40,310
everything from our sessions.json file,

112
00:05:40,310 --> 00:05:44,670
and I've got my URL. Now I don't want my

113
00:05:44,670 --> 00:05:47,340
URL exposed out to the world. I don't want

114
00:05:47,340 --> 00:05:51,000
my sessionList necessarily exposed out to

115
00:05:51,000 --> 00:05:53,100
the world, all right, I just want my

116
00:05:53,100 --> 00:05:55,050
getSessions. And so I can come back down

117
00:05:55,050 --> 00:06:03,246
here, same as before, and say export

118
00:06:03,246 --> 00:06:09,730
getSessions. And remember, so you'll

119
00:06:09,730 --> 00:06:13,050
notice the red underline, that's not how

120
00:06:13,050 --> 00:06:15,140
this works, right? Declaration or

121
00:06:15,140 --> 00:06:18,690
statement expected. Well, that's because

122
00:06:18,690 --> 00:06:25,390
it takes a list like that, right? If I can

123
00:06:25,390 --> 00:06:27,380
export up there just fine, and that'll

124
00:06:27,380 --> 00:06:29,730
work, but if I'm going to export down

125
00:06:29,730 --> 00:06:35,590
here, I have to export as a list, just

126
00:06:35,590 --> 00:06:37,280
like that. And that's what I want to do.

127
00:06:37,280 --> 00:06:39,320
That ultimately is all there is to a named

128
00:06:39,320 --> 00:06:43,500
export, right? So I have created a named

129
00:06:43,500 --> 00:06:46,840
thing and then have exported it. Now I can

130
00:06:46,840 --> 00:06:49,410
change the name, I could come in here and

131
00:06:49,410 --> 00:06:52,430
say as sessions, and that'll work just

132
00:06:52,430 --> 00:06:55,610
fine. And when I do my import, I'm

133
00:06:55,610 --> 00:06:59,650
importing sessions, not getSessions, so I

134
00:06:59,650 --> 00:07:02,020
can rename things right there as I want

135
00:07:02,020 --> 00:07:05,945
to. All right, so that's named exports,

136
00:07:05,945 --> 00:07:08,130
but I have to know the name of things when

137
00:07:08,130 --> 00:07:10,500
I'm importing them, right? And I don't

138
00:07:10,500 --> 00:07:13,380
necessarily want that for my session

139
00:07:13,380 --> 00:07:18,710
repositories, right? I want to just import

140
00:07:18,710 --> 00:07:20,890
session repository. I don't want to have

141
00:07:20,890 --> 00:07:23,330
to know what all the pieces are, so that's

142
00:07:23,330 --> 00:07:30,000
where defaults comes in. And so let's look at what export default meets.

