1
00:00:00,750 --> 00:00:05,880
All right guys are the first pattern we're going to look into is the module pattern and a slight variation

2
00:00:05,880 --> 00:00:08,550
of it called the revealing module pattern.

3
00:00:08,550 --> 00:00:15,630
Now I do want to mention that E-6 or EIAS 20:15 introduced actual modules in javascript meaning that

4
00:00:15,630 --> 00:00:22,020
we can use separate files to export modules which are just custom pieces of code and import them into

5
00:00:22,020 --> 00:00:23,160
a new file.

6
00:00:23,160 --> 00:00:25,760
However this is still not supported in browsers.

7
00:00:25,770 --> 00:00:32,910
You'll have to use a compiler like Babel along with a module loader like web pack for that to work and

8
00:00:32,910 --> 00:00:34,920
we will be getting into that later.

9
00:00:35,010 --> 00:00:40,020
But in the last five we have something called the module pattern that allows us to break up parts of

10
00:00:40,020 --> 00:00:47,940
our code into self-contained modules with private properties and methods or private variables and functions.

11
00:00:47,940 --> 00:00:55,350
So this is actually my favorite pattern and we're going to build a 12 video calorie counter application

12
00:00:55,350 --> 00:00:57,020
with it in the next section.

13
00:00:57,030 --> 00:01:03,210
So let's take a look at how the module pattern works as well as the revealing module pattern.

14
00:01:03,210 --> 00:01:09,180
So basically what we want to do where we have our index each team out here just have a heading and then

15
00:01:09,180 --> 00:01:10,310
we're connecting our app.

16
00:01:10,320 --> 00:01:14,390
J Yes but the way that the module pattern is set up.

17
00:01:14,490 --> 00:01:18,510
Let me just give you a basic structure here.

18
00:01:19,380 --> 00:01:26,940
So we have a module which is going to be in if we can if you guys remember what it is it's immediately

19
00:01:27,330 --> 00:01:32,430
invoked function expression and it runs right away.

20
00:01:32,430 --> 00:01:37,770
So we need to wrap it in parentheses to make it an expression we need to put a function in here an anonymous

21
00:01:37,770 --> 00:01:44,180
function and we need to put parentheses after it to Dick to run it.

22
00:01:44,400 --> 00:01:48,000
And then the idea of the module pattern is to declare

23
00:01:50,280 --> 00:01:59,190
private variables and functions up here and then we have a return.

24
00:01:59,190 --> 00:02:03,630
And what I mean by private meaning that we can't access it from outside the module.

25
00:02:03,840 --> 00:02:12,540
And then whenever we return this is where we declare public variables and functions.

26
00:02:12,540 --> 00:02:12,840
All right.

27
00:02:12,840 --> 00:02:17,050
So that's that's the the blueprint for the module pattern.

28
00:02:17,070 --> 00:02:23,430
Now we're going to have a pretty large project that's going to be a calorie tracker using the module

29
00:02:23,430 --> 00:02:26,570
pattern where we're going to have an item controller.

30
00:02:26,750 --> 00:02:31,560
Each module will be its own controller of an item controller that will take care of the meal items that

31
00:02:31,560 --> 00:02:39,870
we add will have a UI controller that will take care of anything to do with the UI so clicks and form

32
00:02:39,870 --> 00:02:43,010
submissions rendering stuff in the UI.

33
00:02:43,010 --> 00:02:47,880
Anything to do with that will be in the UI controller will have a storage controller that will persist

34
00:02:47,880 --> 00:02:52,950
things to local storage and then we'll have a main app controller where everything will kind of meet

35
00:02:52,980 --> 00:02:55,110
and we'll call an initialiser.

36
00:02:55,110 --> 00:03:03,090
All right so what are you going to do here is comment this out and let's create a module called UI controller.

37
00:03:03,150 --> 00:03:12,890
So UI see TRL and we're going to set that to a module modules so this will be an iffy.

38
00:03:14,340 --> 00:03:20,370
And like I said anything we we declare up here is going to be private so I'm going to create a variable

39
00:03:20,370 --> 00:03:28,620
called text and when I said that to the text of hello world and then all I want to have here is a function

40
00:03:28,620 --> 00:03:31,440
that's going to change this heading right here.

41
00:03:31,440 --> 00:03:34,950
This H-1 to whatever is in this text.

42
00:03:34,980 --> 00:03:38,000
So let's go ahead and create a variable.

43
00:03:38,080 --> 00:03:45,450
So this would be a function expression will create a variable called Change text and we'll send it to

44
00:03:45,480 --> 00:03:46,380
a function

45
00:03:49,080 --> 00:03:54,070
and let's say Konst element and let's grab that H-1.

46
00:03:54,120 --> 00:04:02,940
So document Daut let's do queries selector and we'll just grab it by the tags so H-1 and then we're

47
00:04:02,940 --> 00:04:08,950
going to say elements Daut text content and set that to text.

48
00:04:09,000 --> 00:04:15,380
Ok so we can access any any value any variables we put up here we can access from within this function.

49
00:04:15,780 --> 00:04:17,310
Now both of these are private.

50
00:04:17,310 --> 00:04:24,750
We can access these from outside of this module of the controller module we just put a comment here

51
00:04:24,870 --> 00:04:33,420
saying that this is the standard module pattern and what we want to do is return something from this

52
00:04:33,420 --> 00:04:34,250
module.

53
00:04:34,410 --> 00:04:41,230
OK so what we return is going to be public which is stuff that we can access from outside.

54
00:04:41,400 --> 00:04:54,000
So I'm going to have a function called Call change text and set that to a function and then let's run

55
00:04:54,510 --> 00:05:01,950
change text because we can access the private values the private Boll's the private functions we can

56
00:05:01,950 --> 00:05:05,550
access from within these public functions.

57
00:05:05,550 --> 00:05:10,560
All right so we'll do that and we'll also go ahead and cons. Daut log text just to show you that we

58
00:05:10,560 --> 00:05:13,100
can access that variable directly.

59
00:05:13,530 --> 00:05:26,310
OK so now outside of the module which would be down here we can say UI control or Daut call change text.

60
00:05:26,310 --> 00:05:26,670
All right.

61
00:05:26,670 --> 00:05:31,740
So go ahead and save and you can see that it actually changes to Hello World.

62
00:05:31,740 --> 00:05:36,150
It also logs hello world which is coming from right here.

63
00:05:36,270 --> 00:05:41,600
Now if I were to try to console log actual Just do it over here.

64
00:05:41,810 --> 00:05:48,630
And chrome if I were to take the UI controller and you can you'll see I can do.

65
00:05:48,630 --> 00:05:50,970
Call change tax which you just did.

66
00:05:50,970 --> 00:05:59,310
I can't however do change text if I do that it's going to see change text is not a function because

67
00:05:59,310 --> 00:06:03,060
I can't access this private function from outside.

68
00:06:03,060 --> 00:06:03,360
All right.

69
00:06:03,360 --> 00:06:06,930
We get the same error if I try it over here in fact I'll do that.

70
00:06:06,930 --> 00:06:17,560
It's a UI control word Daut change text and you see I get the same error.

71
00:06:17,710 --> 00:06:28,860
And if we try to access say UI control text we try to access this right here and save that is see that

72
00:06:31,830 --> 00:06:33,410
change sex is not a function.

73
00:06:33,450 --> 00:06:34,730
Let me just comment that.

74
00:06:35,060 --> 00:06:38,930
That's blocking and you'll see that will get undefined for that.

75
00:06:38,970 --> 00:06:41,310
This hello world is coming from this right here.

76
00:06:41,310 --> 00:06:46,530
If I were to comment this so you'll see we just get undefined.

77
00:06:46,880 --> 00:06:51,770
OK so that's that's the foundation of the module pattern we have private stuff here.

78
00:06:51,850 --> 00:06:55,270
And whatever is in this return is going to be public.

79
00:06:55,300 --> 00:07:02,200
So let's go ahead and comment this so and I'm going to give you an example of the revealing module pattern

80
00:07:03,730 --> 00:07:06,990
which is very very similar.

81
00:07:07,000 --> 00:07:15,700
The main difference is that instead of returning our own public functions like we did here you basically

82
00:07:15,700 --> 00:07:23,310
map an object literal and you map to private functions that you want to reveal.

83
00:07:23,350 --> 00:07:29,110
And that's that's why it's called the revealing pattern because you can reveal certain methods from

84
00:07:29,110 --> 00:07:30,940
your your module.

85
00:07:30,940 --> 00:07:39,820
So what we're going to do here use a different example and let's call this the item controller.

86
00:07:39,820 --> 00:07:48,850
So this would be like a databased controller and will set that to a module which is just an iffy function

87
00:07:48,850 --> 00:07:53,290
that invokes automatically.

88
00:07:53,470 --> 00:07:56,380
And let's say let data.

89
00:07:57,100 --> 00:08:01,950
And we're going to set that to an empty array so this can be thought of as as our state.

90
00:08:01,960 --> 00:08:02,320
All right.

91
00:08:02,320 --> 00:08:05,530
And then we'll create private methods here like ADD

92
00:08:08,230 --> 00:08:18,180
so will say add and I'll take an item and then we'll just take that data array and we'll push onto it

93
00:08:18,300 --> 00:08:27,090
that item and then we'll simply just do a console log and we'll say item added

94
00:08:30,170 --> 00:08:37,500
All right now a lot of times you might see an underscore with variables like this because this is a

95
00:08:37,500 --> 00:08:39,440
private variable.

96
00:08:39,780 --> 00:08:44,850
If you remember back in I think it was Section 2 when we talked about conventions for naming variables

97
00:08:45,150 --> 00:08:48,780
I told you that this is you can do this it's allowed.

98
00:08:48,900 --> 00:08:54,990
But you should only do it if it's a private variable and you won't you're only going to deal with private

99
00:08:54,990 --> 00:09:00,780
variables in more advanced stuff like this like using different patterns so that's where you might see

100
00:09:00,780 --> 00:09:01,100
that.

101
00:09:01,110 --> 00:09:01,830
I don't do that.

102
00:09:01,830 --> 00:09:05,040
I don't use the underscore but you might see that in other people's code.

103
00:09:05,190 --> 00:09:09,180
Or you may want to use it yourself.

104
00:09:09,210 --> 00:09:14,910
So let's also create a function called get and forget.

105
00:09:15,000 --> 00:09:17,580
It's going to take in an ID.

106
00:09:18,320 --> 00:09:25,380
OK so it'll take an item id and then I'm going to return the data and I'm just going to use the the

107
00:09:25,380 --> 00:09:34,710
find method here and then passing a value actually will say item and let's add that to an arrow function

108
00:09:34,740 --> 00:09:45,080
and then we'll return let's say return item Daut ID if it's equal to the ID that's passed in.

109
00:09:45,210 --> 00:09:45,510
All right.

110
00:09:45,510 --> 00:09:46,850
So those are adding getting.

111
00:09:46,860 --> 00:09:49,980
Right now these are these are completely private.

112
00:09:50,070 --> 00:09:59,400
So if we go down here and we say return and what we're going to return is an object literal here with

113
00:09:59,490 --> 00:10:08,580
at let's say ADD and set that to add and then say get set to get making those make basically revealing

114
00:10:08,580 --> 00:10:09,790
these methods.

115
00:10:09,810 --> 00:10:20,790
So then we can go down here and we can say item controller Daut add and then let's pass and here an

116
00:10:20,790 --> 00:10:33,230
object with an ID just say Id one name John and save that and we get item added.

117
00:10:33,390 --> 00:10:33,760
All right.

118
00:10:33,780 --> 00:10:44,550
And we could even go in here now and say item controller and we could do get in let's get the item with

119
00:10:44,550 --> 00:10:52,640
the idea of one and it gives us John OK and we can do the same thing over here so will say item dog

120
00:10:52,680 --> 00:11:01,020
gets the idea of one actually will need to console log that

121
00:11:06,670 --> 00:11:15,390
flips item controller OK and I could add another item here if I wanted.

122
00:11:15,430 --> 00:11:23,650
I don't know why items are people but that's fine I'll say Mark and then we'll get the item with two

123
00:11:24,370 --> 00:11:25,690
and you can see we get Mark.

124
00:11:25,690 --> 00:11:32,840
Now if I were to not have this right here if I comment that out that's going to make make this private.

125
00:11:32,890 --> 00:11:36,430
OK if we don't include it in this return it's going to be private.

126
00:11:36,430 --> 00:11:40,650
And if I save we're going to get an error saying that it's not a function.

127
00:11:40,660 --> 00:11:40,990
All right.

128
00:11:40,990 --> 00:11:46,020
So that's one of the main differences between the revealing module and the standard module pattern is

129
00:11:46,030 --> 00:11:52,480
that we're mapping or we're returning an object literal that directly reveals methods that are inside

130
00:11:52,480 --> 00:11:53,400
of the module.

131
00:11:53,500 --> 00:11:53,770
All right.

132
00:11:53,770 --> 00:11:55,910
Hopefully that makes sense.

133
00:11:56,080 --> 00:11:58,630
Now this looks cleaner.

134
00:11:58,630 --> 00:12:04,410
The revealing module pattern does look cleaner but this allows you to do more because you can.

135
00:12:04,420 --> 00:12:10,720
You know you can call change tacks but you can do other stuff as well like console log something without

136
00:12:10,720 --> 00:12:13,750
having to include it in your actual private method.

137
00:12:13,750 --> 00:12:20,000
So I mean it's all up to you what you want to use and it depends on what you're building as well.

138
00:12:20,020 --> 00:12:23,820
So you know it's all up to you.

139
00:12:23,920 --> 00:12:30,100
My job is to just introduce you to these patterns in these ways of doing things and then you know you

140
00:12:30,100 --> 00:12:32,590
make your choice on what you want to do.

141
00:12:32,590 --> 00:12:35,430
All right guys that's going to be it for this video in the next video.

142
00:12:35,430 --> 00:12:41,230
I want to talk about the singleton pattern which is actually a very another variation of the module

143
00:12:41,230 --> 00:12:48,460
pattern that uses an iffy but it has it's a little different where you can it's use to create objects

144
00:12:48,490 --> 00:12:51,680
but you can only instantiate the object once.

145
00:12:52,090 --> 00:12:54,130
So we'll get into that next.

