02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Observer- adding and dispatching events

The name observer might not sound familiar but your probably working with it a lot. If you ever worked with an event you've been working with an implementation of the observer. The observer enables us to let an object update other objects on changes in it's state with it out knowing explicitly who is listening to it.

Video Transcript

00:00It's time for us to meet another design pattern a behavioral design pattern at that and that is the observer design pattern
00:08the observer
00:10The observer below the observer
00:13Announces events without explicit communication with the objects. It's announcing to the idea is basically you're asking from
00:22When you you're basically asking to be updated the concept is split down into two
00:28Points there in is there is an event dispatchment and then there's listeners
00:32So there's all this group of objects that say hey, I want to know when a certain event happens
00:36They explicitly ask for it, but me as the object itself. I'm not aware who's listening to me
00:41I'm just passing on the information to whoever requested that information with that said in this lecture
00:48What we're gonna do is we're gonna build an event dispatcher using a decorator mixing together two different design patterns
00:54So let's jump right into it
00:57Long time ago
00:58We met the decorator design pattern as we created the self destruct decorator a really long time ago in this lecture
01:06we're gonna create the
01:08observer design pattern which is more commonly known in JavaScript as
01:12An event dispatcher we're gonna create our own event dispatcher now to do that
01:18We're gonna use the decorator logic by adding extra logic to objects
01:22So with that extra up the extra logic is going to be an event dispatcher, and I'm gonna go ahead here
01:28I'm gonna call this event dispatcher
01:31decorator
01:32And I'm gonna send in an object
01:34That's our first item the next stage is I need a list so I'm gonna go ahead and create this private variable
01:40It will only be accessible for these methods that we're going to be adding into the object
01:45So what are these methods first of all there's the add event listener?
01:49But I like calling it just add event because it's shorter and cleaner
01:54The add event itself will accept two parameters one is the type of the event and the next is going to be the callback
02:03Beautiful now callback is probably not the perfect name, but it's the best name that I could put there. I actually call it listener
02:11Because a callback is when you're only sending one function and the listener is when you have a lot of
02:16Callbacks in many ways. All right, so you have your type
02:19We have your listener the next step that we want to do if we're gonna create an ad event
02:24What I'm trying to do is basically create a list of all the events based on their type and based on if they're already there
02:30Or not. So first step is I want to do that. I want to do is check to see if list type exists
02:37If it doesn't exist then it's time for me to create it. So if it does not exist
02:41We're gonna go ahead and just create it very quickly
02:45List type and I'm gonna just go ahead here and create an array right inside there
02:52The next step is I want to check to see if this function that we're sending in is already inside of that list if it is
02:58Then we're not going to do anything. But if it isn't then we want to add it in
03:02So let's go ahead and check that if our list type
03:06Which is an array. We know that because we created it in line 85. Let's ask for the index of
03:14Listener
03:15Now the index of should return a value between 0 and the actual value and if it does not find it then it's going to return
03:22Minus 1 and that's exactly what we're looking for. We want to make sure that it actually
03:28Explicitly is equal to minus 1 and if it it means if it is does is equal minus 1
03:33Then it means that this listener is not inside of our list now that we know that it's not inside of our list
03:39Let's go ahead and add that
03:41Listener by just pushing it right into it. And there we go. We just created our add event listener
03:49logic, all that's left for us to do is also create a
03:53Dispatcher now you probably are familiar with it already
03:57But I'm gonna go ahead and talk about it really briefly. Anyways a dispatch event dispatch
04:03Event an event dispatcher is basically a mechanism that enables you to dispatch the event and trigger
04:09Basically all these stuff that we're listening to to be activated and be called and we use that using a dispatch event many languages have that built in
04:17Some features in JavaScript have it built in some features
04:21Don't and this enables us to add that feature to it
04:24Even if it's not natively supported such as in our objects that we're creating ourselves
04:29So our event dispatcher what happens in the process of an event dispatchment?
04:33It's the standard that inside of our either we're gonna actually get a property called
04:39Type just like the type that we were sending when we're adding events
04:43so what I'm gonna do is I'm gonna get a reference to the actual list itself a
04:47list and what I want to do is I want to get an access to that list and
04:52What do I want? I want the list of the e type
04:55Literally getting that specific list now if that list does not exist
04:59So I'm gonna a list that only if it exists, then we're actually gonna do something
05:04If it does exist
05:05What we want to do is we want to loop through that array and literally trigger all of the different methods that are
05:10Functions that are sitting inside there. So let's go ahead and do that for var I
05:15in
05:16or I
05:18in a list
05:20Let's call it index because I'll make it look better index in a list. What do we want to do?
05:26Not a prototype. What do we want to do? We want to go to a list
05:30index and
05:32We want to just literally just trigger that function
05:34All that's left for us to do is also send in that e to be able to fully qualify our event
05:41Dispatchment now before we're done and only if our a list exists
05:45what I want to do is I want to basically make sure that e dot target exists, which is another standard that exists inside of
05:54Dispatchers where the target usually will give us the objects itself that is dispatching the event in this case
05:59It would probably the oh, so if e dot target does not exist, what do I want to do?
06:05Well, what I want to do is I want to go to e target and I want to assign it the value of this which will
06:09Be the object that has just been decorated
06:12So that's more or less all we really need to do to create an event dispatcher and to test this out
06:18I just want to go ahead here right after our event dispatcher decorator here
06:23And I'm going to go in line 107 here for a second creating very quickly a variable of oh, and I'm just going to
06:29Create it a completely empty object there
06:33Now that this object is completely empty. I'm going to go ahead and decorate it by calling the event dispatcher decorator
06:40So let's go ahead and do that and I'm going to just go ahead here and decorate it now that it's decorated
06:46It has the capability and capacity to dispatch event. Let's go ahead and add an event to it
06:51So I'm going to go to o add event and I'm literally going to say what event I want to say event
06:57over and
06:59What do I want to do? I want to call this function
07:03Console log
07:05It's over
07:08All right
07:08All that's having me to do is also dispatch them and usually you wouldn't do this one after each other
07:13But we're just testing out to see if everything is working
07:15so I'm going to have your dispatch event and I'm going to literally just create here a new object and I'm going to set the
07:23the
07:25Type to be over and I'm not going to put anything else beyond them
07:30We're just going to test it out
07:31Just the way it is and if everything is working according to plan
07:34Then it's going to go ahead and actually based on that type
07:37It's going to find the type and it's going to actually trigger it
07:40So first of all, we created the object we decorated it once we decorated we added an event and then whenever that dispatch event
07:48Happened it should trigger that it's over method now just to wrap this up
07:52Oh, so event dispatches I could add many functions and notice
07:56This is not the same function because it's a completely new one. So it's over to
08:01Contrary to that if I stored this as a
08:04Variable and I stored that variable right there and let me go ahead and do that just for this presentation. So this is a fun
08:11Shin
08:12Function and I'm gonna just cut that put that right there
08:16So I have your a function that's stored as a variable and I'm sending it in as a property notice
08:21This is it's over to and I'm going to go ahead and call it not once
08:24I'm going to call it three times adding that event three times
08:27But technically we want to make sure that it's only going to be called once. Let's see if it works
08:31so let's go back into our application and I'm just going to go ahead here and click on refresh and
08:37If I look into my application, I'll see it in my console
08:40It's over and it's over two and I'm not seeing it's over two three times
08:44Exactly as I wanted it to be now
08:46We're not done with the event dispatcher or with the observer design pattern yet
08:50One thing that's missing here is that we don't have a way to remove events and we haven't really integrated into
08:57Application which is what we'll do in the next lecture

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!