Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Start a free Basic trial
to watch this video
Events and Messaging in JavaScript
46:55 with Doug NeinerThis talk will serve as an introduction to event and messaging patterns in modern web sites and applications. Starting with a review of DOM Event handling and some common difficulties, this talk will advance through to discussing observable objects and wrap up with an introduction to the message bus. Following this logical progression you will become familiar with common eventing and messaging patterns in JavaScript and you will even be exposed to actual implementations of those patterns in popular JS frameworks.
-
0:00
[MUSIC]
-
0:13
>> So I need to participate in modern web conference today.
-
0:16
Got a lot to cover.
-
0:17
And so we'll jump right into it.
-
0:21
All right you all have this at the end so don't worry about writing anything down.
-
0:25
The only thing you'll want to write down towards the end is the email address, but
-
0:28
when I share the link it won't have that in there.
-
0:30
But anyone here, if you have a question about something that I talked about Or
-
0:35
just wanna e, e, you know, e-mail anything about JavaScript in general,
-
0:38
feel free to reach out to me directly.
-
0:40
I also love FontAwesome.
-
0:41
I started using the icons, realize I'd just give them a call-out.
-
0:44
I use it on all my projects and saw someone in the chat mention it earlier.
-
0:48
It's a blast.
-
0:50
I live in Iowa with my five kids and
-
0:52
my wife, and we're, you know, here at home right now,
-
0:55
so if you do hear any stomping or anything, it might be children upstairs.
-
1:00
All right, I work at appendTo, I'm not gonna belabor this point since Ralph
-
1:03
already covered this, but I love what I get to do,
-
1:05
work with some great people, I work from the comfort of my home.
-
1:08
So I really don't, I, just so many great things to say about appendTo and
-
1:11
I've really enjoyed the innovative things I've got to do while I'm here.
-
1:16
All right let's talk.
-
1:17
We're gonna talk about three primary things.
-
1:19
The DOM, we're gonna deep dive in the DOM events, kinda use it for a foundation for
-
1:23
messaging in our apps outside of the DOM.
-
1:25
We're gonna talk about the observer pattern and
-
1:28
how to use that with your objects.
-
1:29
And then right towards the end, we're gonna touch on the message bus.
-
1:32
This is certainly not gonna deep dive into architectural pieces about how to
-
1:37
arrange the entire, you know, entirety of your application.
-
1:40
All right, so, what is an event?
-
1:44
I went to the dictionary to make sure I could properly explain this and
-
1:47
this is what I found.
-
1:48
A thing that happens.
-
1:50
Especially one of importance.
-
1:52
Wow, man, I could, I could probably get in to writing definitions for
-
1:55
dictionaries if that's required.
-
1:57
A thing that happens.
-
1:57
So I found that very enlightening.
-
2:01
But it's true.
-
2:01
It's event is simply a thing that happens.
-
2:03
We have a lot of things that happen in our calendars.
-
2:06
We have a lot of things that happen in our life We share those with other people.
-
2:10
So in real life, we kinda understand the concept of an event.
-
2:13
But in the technical realm when we're talking about JavaScript events or DOM
-
2:17
events they're generally talking about a notification about the event I just wanna.
-
2:23
We're gonna be using the word.
-
2:24
I'm going to be using the word event today.
-
2:25
To use both together.
-
2:26
But really we're not handling the click someone already clicked.
-
2:30
We're handling the response to that.
-
2:31
And we're going to be able to respond to that notification.
-
2:34
And the same thing is true in our code.
-
2:36
We're not causing a, an event by announcing that it happened we're,
-
2:40
we're just notifying about what happened.
-
2:44
The important thing about an event is that it gives us voluntary visibility into what
-
2:48
would otherwise be an input only system.
-
2:51
I originally had closed system.
-
2:52
I didn't wanna be too specific.
-
2:54
Because a lot of times can have methods and really have
-
2:58
tight coupling between components that know about their internal methods.
-
3:02
But in general events give us that voluntary visibility into a unit.
-
3:06
Otherwise that would be operating on its own.
-
3:07
All right, so what do Facebook Twitter and Instagram have in common?
-
3:14
Well, they are your friends, or maybe not your friends, people that have added you,
-
3:18
and you've accepted that are notifying you about events in their life.
-
3:23
Again, it's voluntary.
-
3:24
You don't have to check those services.
-
3:26
You can voluntarily respond to those things, but these are a nice real-life
-
3:30
example of paying attention to things that happen, and then doing something,
-
3:33
maybe it's responding, maybe it's reading an article that was shared on Twitter.
-
3:37
But we have these examples in real life,
-
3:39
of things that are event-based, that we respond to.
-
3:44
All right, so, deep-diving into DOM Events, I'm sure this will be review for
-
3:48
many of you, but
-
3:48
there, hopefully you'll pick up things that you might not have known before.
-
3:52
If not, just to reiterate why DOM Events work is such a great platform for us.
-
3:57
You know, moving that out to our own application code, and
-
4:00
using some of the same patterns.
-
4:01
All right, a DOM event,
-
4:04
when you wanna be when someone clicks a button or it resizes their browser.
-
4:10
Xhr requests trigger events.
-
4:11
A number of things trigger events when we're dealing with HTML and the browser.
-
4:17
Going way back to early, early, early DOM specifications you would just add a,
-
4:22
a handler using an attribute and
-
4:23
then you'd have some sorta global function in your code that would do something and
-
4:26
you might even have the whole code right there in the onClick handler.
-
4:29
May not even be calling the function.
-
4:31
Obviously, big problems with this.
-
4:32
There's no separation between your HTML and
-
4:34
your JavaScript, so hopefully we're not doing that.
-
4:37
Anyone here's not doing that.
-
4:39
But it does, pollutes the global namespace.
-
4:41
There's number issues with this.
-
4:43
You can make it a little better by grabbing the element by its, you know,
-
4:46
an iclass name or id.
-
4:47
I just used id here.
-
4:49
And assigning it.
-
4:50
But it's still the same problem, just kind of changed a little bit.
-
4:53
Now we only can ever have one handler.
-
4:55
So if we attach it using this old method, which still works.
-
4:58
But if we attach it, and
-
4:59
someone else tries to add a collective event using the same pattern,
-
5:02
it would just remove ours, and theirs would be the one that gets used.
-
5:05
So this still isn't the solution.
-
5:07
So this is where, why we use addEventListener.
-
5:11
And I'll touch on the IE specific one in just a minute.
-
5:14
But this is why we addEventListener.
-
5:16
And there's just three parts: The event name, the callback that's gonna happen in,
-
5:19
and it'll pass a parameter with date details about that event.
-
5:23
And then the last parameter, which is going to signify whether you
-
5:26
want to have this execute in the capturing phase or bubbling phase,
-
5:30
which we'll talk about in a few slides.
-
5:33
This doesn't work in IE8 and under.
-
5:36
so, the cross-browser one, if you've been deep diving any of
-
5:38
the source code of libraries that abstract this, or maybe have written this yourself.
-
5:43
You test using feature detection, like Dave talked about earlier.
-
5:46
Say, does it have the method we want?
-
5:48
We bind the click handler.
-
5:50
And then, otherwise, we use, attach event, if it's present.
-
5:52
And that's the IE specific one for, again, IE8 and under.
-
5:56
The important thing is,
-
5:57
there's a lot of additional pieces here that don't function quite the same way.
-
6:01
The context of this inside of that callback attachment is not equal to
-
6:05
the element like you might be used to.
-
6:08
the, that doesn't get the parameter with the event,
-
6:10
that's available in window event.
-
6:11
So there's a number of changes which is why most of the time if
-
6:14
you're handling those older browsers using a DOM abstraction library like
-
6:18
jQuery makes a lot of sense.
-
6:20
Or something that does the same amount.
-
6:24
If you aren't targeting those versions, you're working in nine and
-
6:27
above, just use an addEventListener and then you can use both the capturing and
-
6:30
the bubbling we're gonna talk about, next.
-
6:34
Or soon, I should say.
-
6:35
Not next.
-
6:37
okay, so I'm, I'm coming into this talk with the understanding you've
-
6:40
probably been using events for a while in your JavaScript code.
-
6:43
You've been buying into clicks and mouseovers and
-
6:45
all these things to wire up your user interface.
-
6:48
so.
-
6:49
I'm going to just talk about some shared concepts because some of those same
-
6:53
concepts you're using will transition when we talk about messaging later in the talk.
-
6:57
The fact that it subscribed and
-
6:59
unsubscribed based on the event name that's a that will carry directly over.
-
7:04
We'll a lot of times call them topics if we're dealing with a message bus or
-
7:07
events with an event emitter.
-
7:09
But it's gonna be the same concept.
-
7:11
They often include a payload of information data additional meta
-
7:14
data about the event that happened.
-
7:16
This is a common pattern and
-
7:17
in a lot of times is why these events are hard to tell you that level of detail.
-
7:22
So you might be able to find out which key was pressed, on a key press.
-
7:25
[BLANK_AUDIO]
-
7:30
The event.
-
7:31
And then some, depending on what library your using to add events.
-
7:36
Some of them will give you the similar control over, you know stopping immediate
-
7:41
propagation or, or keeping or canceling it or changing default behavior.
-
7:45
Some of those will carry that over.
-
7:47
Your mileage may vary on the transferability of that.
-
7:51
Some of the key concepts.
-
7:53
That I wanna just deal with in this talk is making sure you truly
-
7:56
understand capturing and bubbling and,
-
7:58
and what happens where and for me, just understanding capturing really helps you
-
8:03
understand how jQuery can do some of the things it does, which is kind of cool.
-
8:06
And be able to do that in your own code, maybe without using the library.
-
8:10
One other challenge is just making sure you fully understand, based on that.
-
8:13
Event delegation how it works.
-
8:15
And then we'll deal with the specifics of,
-
8:17
of some other things with target and current target.
-
8:21
All right, so, we have a example structure here.
-
8:25
We're gonna start with our window.
-
8:26
Then we have our document.
-
8:27
Then we have the root element, which is HTML.
-
8:30
Then our body.
-
8:31
Then our header and then our logo.
-
8:34
So we have this structure of giving obviously just a slice of it.
-
8:37
But we'll use this to talk about capturing and bubbling.
-
8:41
So if we add an event listener to one of these elements using click we'll pass it
-
8:44
call back but we pass true instead of false.
-
8:47
That's that final parameter.
-
8:49
If we pass true it's going to capture an event during excuse me,
-
8:53
it's going to trigger the call back during the capturing phase of the event.
-
8:57
And this goes back, there's a huge history you can read about it on quirks.
-
9:00
Quirks mode on his website it talks about the history here.
-
9:03
But the idea is that it will start capturing starts at the top
-
9:06
most element in your, in your Dom.
-
9:09
In this case is gonna start with window, then document,
-
9:11
then go on down until it gets to the element that you started the event on.
-
9:17
So it starts, starts at the top.
-
9:19
Works the whole way down to the element that you might've clicked.
-
9:21
Which would be logo link.
-
9:22
Before it then switches and bubbles, which is the other direction and
-
9:27
then goes the whole way up.
-
9:28
90% of the time when you're doing something if you're using jQuery any of
-
9:32
those other pieces, you're using bubbling.
-
9:34
So that's where,
-
9:35
if you're already familiar with delegation and attaching handlers higher up.
-
9:39
You're expecting the event to move up and eventually get to where you can handle it.
-
9:43
Capturing does the exact opposite and happens first.
-
9:46
So it starts at the top, works its way down, and stops and
-
9:49
goes the other direction and works its way back up.
-
9:52
This is really cool we'll do a quick demo here.
-
9:57
I'll move over to the screen you're on.
-
9:59
If I click out here, just in the document, you're going to see it triggers on HTML.
-
10:05
I'm going to click this again, it will go away, so let's pay attention.
-
10:07
Capturing on window then document then HTML, and then it reverses and
-
10:11
goes back up.
-
10:12
I add the header, which is from our example.
-
10:15
We get Window document HTML header.
-
10:17
And then it goes the other direction back up, and that's the bubbling phase.
-
10:21
The whole way down to our anchor, if we had it included there.
-
10:24
So it gives you two different times where you can handle that event.
-
10:28
Once on capture, on the way down they're on the way up.
-
10:32
It's important to note, I'm gonna go back two slides here,
-
10:35
when you do provide this, if you're not on the element that was clicked.
-
10:39
So if this wasn't being bound to that logo, and it was one of the parents,
-
10:42
it will only fire this callback during the capturing phase.
-
10:46
In the bubbling, if we had it on one of the parents, would only fire the call,
-
10:50
callback during the bubbling phase.
-
10:52
If you had a event list and with either false or
-
10:54
true on the actual element that was clicked, it wouldn't matter.
-
10:57
It's gonna fire regardless because it's the during the context switch between
-
11:01
capturing and bubbling is when it's on the element that it was triggered on.
-
11:04
So whether you put true or false in that setting, it's going to work.
-
11:07
Either way.
-
11:11
The important thing here to know is that not all events bubble and
-
11:14
if you've been using JQuery a lot and it abstracts some of this and
-
11:17
makes all of the events bubble, you may not be familiar with this.
-
11:20
But not all events bubble.
-
11:21
You can check that using your event parameter, and
-
11:24
I just have mine named E here but you can just name that and
-
11:27
check for e.bubbles and it will tell you whether or not that event will bubble.
-
11:31
So if we attached an event listener or
-
11:33
focus to an input, it's gonna say false, that will not bubble by default.
-
11:37
And that's why jQuery gives you focus in and focus out.
-
11:40
It gives you that ability due to the fact that it won't natively bubble.
-
11:45
However, this is really cool, events that do not bubble can still be captured.
-
11:50
So if we were to add a focus handler to the document and
-
11:53
put true in the second part.
-
11:55
True right here.
-
11:57
Then when we input, focus on an input the document level so
-
12:02
in a way delegated it will pick up the fact that, that input has been captured.
-
12:06
And if you go and deep dive the source code of j Query you'll see that's
-
12:09
exactly how they're able to implement some of those.
-
12:12
Features that other, otherwise wouldn't bubble.
-
12:15
So that's kind of a, a fun thing but it's something you can use as well when you're
-
12:18
in those edge cases and you just can't get access to something that you need.
-
12:23
StopPropagation is literally just saying once it's on its way up, stop.
-
12:28
And so, if, if an element was chosen, and you said stop propagation on
-
12:32
that callback, the parent would not receive a notification about that event.
-
12:36
The difference between the two, stop immediate propagation, is that any
-
12:39
other event listeners bound to that same element would also be stopped and
-
12:44
they would not fire.
-
12:45
So, if you had three click events mount at different times in the first one
-
12:48
calls stopImmediatePropagation, the second two would not fire.
-
12:53
Now, as a gotcha Oh, you know, what,
-
12:56
we're gonna talk about the gotcha in a minute where the event delegations are.
-
12:58
I'm jumping ahead of myself.
-
13:00
Events bubble.
-
13:01
Right? I discovered that in the last few,
-
13:03
few minutes here.
-
13:04
Why is that useful?
-
13:05
Now that's where event delegation comes in.
-
13:08
And I'll read this right off the slide but it's the practice of leveraging event
-
13:12
bubbling and adding a single event to an ancestor.
-
13:15
You know example would be the document.
-
13:17
Instead of adding multiple events directly to the children.
-
13:21
So here's a picture of that.
-
13:23
If we had all these child items and we attach click handlers to each one of them,
-
13:27
we'd have to first receive a reference to them.
-
13:30
And then be able to attach the click handlers to each one of those.
-
13:34
As opposed to we could attach one flick handler to the container, and
-
13:37
it would automatically because of event bubbling,
-
13:39
get access to all of those separate click events.
-
13:42
So event delegation is really powerful.
-
13:44
It's a great concept to make sure you leverage in your code to
-
13:47
keep things clean and fast.
-
13:50
Some of the modern day frameworks do this for you automatically.
-
13:53
Back1 I know is one of them.
-
13:54
React from Facebook does this automatically.
-
13:56
And theirs is even more complicated cuz of their it's a,
-
14:01
it's not even, it's a synthetic events system.
-
14:05
Some of the benefits, I'm not gonna spend too much time on this right now,
-
14:07
but there's less DOM traversal during setup.
-
14:09
You're not having to get a reference to each item that you wanna apply an event
-
14:13
handler to.
-
14:14
There's no cleanup needed when the child elements are removed.
-
14:17
You don,t have to stop listening or, or unbind something.
-
14:21
New child elements work immediately.
-
14:22
So if you're loading data in via Ajax or using local templating all of those
-
14:27
event handlers don't have to be bound and rebound for that new code to work.
-
14:30
As long it matches the selector or
-
14:33
the test that you're running against it, it will work.
-
14:37
If you're familiar with JQuery and you've used delegated events before then you
-
14:40
might find this next part interesting because we'll I'll show how to do it
-
14:43
without JQuery, and so you can kinda get an idea of what it's doing.
-
14:46
It has a little bit more streamlined, it has a number of tests in there for
-
14:49
browser support and it will use things like matches and matches selector.
-
14:53
Where I'm just gonna do simple tests based on tag name or or maybe an attribute.
-
14:58
A lot of code here.
-
15:01
But we'll talk through it a little bit at a time.
-
15:03
So this is the code that we'd be running.
-
15:05
This would at the end of the day,
-
15:07
where it'd be attaching to the document click handler.
-
15:09
That we're looking for a button that has an atribute of data ID.
-
15:13
That's the longest road of what we're doing.
-
15:15
And then it's gonna do something with that ID.
-
15:18
So, the first step we're gonna have is we're going to bind an event handler to
-
15:21
the ancestor element.
-
15:22
So, in this case the ancestor element is document.documentElement.
-
15:26
This is a quick reference to each HTML.
-
15:27
So, you don't have to do find owner by tag name, or
-
15:30
do anything crazy to get the HTML element.
-
15:32
Just document.documentElement is the HTML element.
-
15:36
And is available as soon as you have code running.
-
15:40
So that's the first thing, we'll attach a click handler there, and
-
15:42
we'll make sure we have e so we can have it available, so
-
15:45
we can interact with the event object that will come through.
-
15:49
Then we're gonna try to decide as quickly as possible,
-
15:51
because this will handle all click events on the page.
-
15:53
We're gonna try to handle as quickly as possible if we want to do
-
15:57
something with this particular event.
-
15:59
So the thing I'm using here is,
-
16:00
I'm grabbing the target which we'll talk about in a minute.
-
16:03
Look at it's tagging, making it lower case because there will be a difference in
-
16:07
what that is name using a an XML base document versus and HTML bas document.
-
16:12
So, my check for being button.
-
16:14
And then, the next thing I'm gonna do is try to get it's data ID and
-
16:17
it doesn't have one, I'm gonna return.
-
16:19
Now, if we wanted to there's.
-
16:23
A rel, you know, it's not rel.
-
16:24
It's not super new.
-
16:24
But it's another way of doing this is a lot faster which is calling matches.
-
16:29
In the latest version of Chrome and Opera, you can just call matches.
-
16:32
In older versions you can look at can I look at details on this?
-
16:35
But there is available VF prefix.
-
16:37
So for IE9 and up, you can do this.
-
16:39
While there is MS modem web kit.
-
16:42
And that's another great use for modernizer to be able to get
-
16:45
the correct prefix for the uh,for method that you wanna use.
-
16:48
But, in this case, all we have to do is call target matches button and
-
16:51
say, does it have a attributed data ID?
-
16:53
If it does, then we wanna handle it.
-
16:56
And then I can confidently get the id and then do something with it.
-
16:59
So it kind of shortens up our code considerably.
-
17:05
All right, finally the last thing we're gonna do is cancel the default which if we
-
17:08
were delegating on a link for instance or
-
17:11
a button that would've submitted a form, we can prevent the default.
-
17:14
We obviously don't want to do that at the top of our method before we know if
-
17:16
we should be handling the delegation.
-
17:18
This is at the point at which we're back to almost using a normal event handler.
-
17:23
Once we've ruled out anything that doesn't match what we want we know we have it
-
17:27
now we can kind of continue as if we're a normal event handler.
-
17:32
One more thing, I won't belabor this for sure.
-
17:34
When I share the links later there's a link to an article I wrote years ago on
-
17:37
fourier coding that explains why and when you might want to use the term false or
-
17:41
when you're not gonna want to use it.
-
17:44
And so you can read that.
-
17:45
But it, long story short will stop propagation and
-
17:48
prevents default which breaks delegation if you're planning on using that.
-
17:54
All right, so one of the challenges with event delegation, one of the challenges
-
17:58
here is that if you were to have a click event that you want to handle
-
18:04
on this lowest level, let me actually select from the screen you can see,
-
18:07
the lowest level here, and you wanna delegate it to document.
-
18:12
But you wanna stop propagation so it doesn't maybe trigger a run on body or
-
18:15
trigger a run on HTML.
-
18:16
You can't do that.
-
18:17
Because if you consider bubbling, even though it's fired here,
-
18:21
that's the source element.
-
18:22
It's gonna, by the time your handler gets it,
-
18:25
it's already propagated the whole way up to the document.
-
18:27
And now you're handling the delegated version of that event.
-
18:30
So stopping propagation will keep it from moving to the window, but
-
18:34
it wouldn't stop it from moving up the tree here from header to body in HTML.
-
18:37
So that's one gotcha just to make sure you pay attention to.
-
18:41
The second one is that target, event target.
-
18:43
It may not be what you think it is, so let's talk about what that is, and,
-
18:47
and what it means.
-
18:49
Target is a reference to the element where the event originated.
-
18:52
So if you clicked on a link, it would be the anchor tag.
-
18:54
If you clicked on a button, it'd be the button.
-
18:57
Current target is a reference to the element that holds the event listener.
-
19:01
So in that case, it's document if we bound to that or one of the parent elements.
-
19:05
It will be the same as this, unless you've actively changed the.
-
19:08
Context of your callback, which happens when you're using it with a library or
-
19:12
with a component.
-
19:13
So e.currentTarget is the object that's the parent.
-
19:16
That's the one you have event delegated to, and then target's different.
-
19:19
There's some notes, if you're using attach event,
-
19:21
there's no equivalent to currentTarget, and
-
19:23
there is an equivalent to source element, which is available on window.event.
-
19:29
Let's look at some more code here.
-
19:30
So here's where I'm gonna use just a few Bootstrap classes for
-
19:32
our button, so it looks a little nicer.
-
19:35
Let's talk through the code, we're gonna grab just using query selector to
-
19:38
grab a single reference to this outside wrapper.
-
19:42
We're gonna add an event listener so we're delegating and event to this wrapper.
-
19:46
And we're gonna just check to make sure we clicked on a button, and
-
19:48
then we're gonna display the date.
-
19:50
So something very simple.
-
19:51
We click the button, the date's displayed, but we're doing it through delegation.
-
19:55
Run the demo.
-
19:57
Click the button, and I see the date, which is great.
-
19:59
This is what we want.
-
20:01
We click here.
-
20:02
Again, it doesn't work.
-
20:04
And so it's somewhat sporadic, we don't know why.
-
20:07
But let's go ahead and
-
20:08
check our, our we'll do some debugging here in the browser.
-
20:12
So again alert is not a debugging tool, unless you really, really need it.
-
20:17
But console log is.
-
20:18
So we're gonna use that.
-
20:19
We're just gonna, instead of doing anything else in here,
-
20:21
we're just gonna check what's the target, and what's the current target.
-
20:26
So if I click on the, this DIV out here that we're binding to,
-
20:28
it just says the target is DIV and the current target is DIV.
-
20:31
If I click on the button, you'll see it targets the button.
-
20:34
But actually I was clicking on the icon, and that's where our problem was.
-
20:37
Because the icon is not a button.
-
20:39
A parent of the icon is the button that we're interested, interested in.
-
20:44
Well, you're not gonna like the solution.
-
20:45
This isn't a, this isn't a good solution.
-
20:47
This isn't something I would use in production.
-
20:50
I'll show you.
-
20:50
It's a much more complicated, you'd abstract this out,
-
20:53
and once you've started going down that road, you should just use a library, and
-
20:56
I'll show you how to do it in jQuery.
-
20:58
But in this case I'm just gonna check whether it's a button, and
-
21:01
then I'm gonna check whether the parent's a button,
-
21:02
cuz I know that there's not gonna be multiple nest elements in my button.
-
21:06
So yeah, I am using the word target a lot.
-
21:08
I see that in check.
-
21:09
[LAUGH] Anyway this will fix our problem because we're checking whether it's
-
21:14
the icon or the button in, in both will work.
-
21:18
So now, once you've seen how to do it all yourself, this is why you go back to
-
21:22
the library and say wow that looks a lot whole cleaner, and it does it all for me.
-
21:25
So this is what it would look like with jQuery.
-
21:27
We just have the click event.
-
21:30
We checked. This would be the selector.
-
21:31
It's gonna put through to make sure it matches that selector.
-
21:34
And then if it works it will, it will run our, our callback.
-
21:38
So in this case this works out of the box of what we'd expect it to.
-
21:41
But I thought it's important that you know how to do it yourself,
-
21:44
if you ever need to.
-
21:45
Or just to understand what's going on behind the hood in some of
-
21:47
the abstractions you might be using.
-
21:52
All right so, just went into the very detailed pieces about targets and, and
-
21:56
bubbling and, and delegation.
-
21:59
But let's take a step back now and look at, we use these all the time right?
-
22:02
How else would we interface with our controls on a page if we
-
22:05
didn't have events and then the methods to control them?
-
22:08
One thing we never expect though is that a button on a page is gonna reach out and
-
22:12
talk to some of our internal app code.
-
22:14
We don't ever have that expectation.
-
22:16
We know that it's going to concern itself with what it needs to do, and
-
22:20
only what it needs to do.
-
22:21
So that's a great pattern where it's completely self contained it has a single
-
22:25
responsibility concept built right in and it's not gonna
-
22:30
do anything with our internal code, it has no knowledge of how it's being used.
-
22:33
It just, knows that it needs to work a certain way.
-
22:36
Which means our, we've also learned that components that expose meaningful events
-
22:40
are easy to work with.
-
22:41
So that's the output from the component, our meaningful events,
-
22:43
whether that be a key press or keydown or
-
22:46
keyup in an input field or whether that be something more abstracted.
-
22:51
Like a return to a XHR response.
-
22:53
But its gonna give us these meaningful outputs.
-
22:57
And then they also take a meaningful set of inputs and
-
23:00
that's the method API that you expect to work with.
-
23:03
And this is where our frustration lies between output and input, and
-
23:06
cross browser compatibility when they don't do the same thing.
-
23:09
When an event in one browser gives you more information or
-
23:12
gives it under a different attribute, in another browser And so,
-
23:15
when they're consistent and they work, man this is how we build stuff.
-
23:19
So that's fantastic.
-
23:20
These are great patterns.
-
23:21
It's interesting that this pattern has a name.
-
23:24
And the question is why don't we write our code like this everyday if it's so
-
23:28
great to work with.
-
23:28
We don't have an option with browser code.
-
23:31
We can't dig into the internals and do something different or
-
23:33
expect it to do something different.
-
23:35
So why don't we write code like this all the time?
-
23:39
Well it's called the observer pattern, and
-
23:41
it's when you have a reference to another element, and then you indicate that you
-
23:45
want to be notified when it has an event that you're interested, interested in.
-
23:49
So the object itself, that's holding all those subscriptions or,
-
23:53
or that will be triggering the events, doesn't care.
-
23:56
About who's subscribing, it just knows that someone subscribed, and
-
24:00
they wanna know when I trigger the event click.
-
24:02
Or when I trigger something different.
-
24:04
It doesnt' care if there's 100 or one subscribers.
-
24:07
It doesn't, it just honestly doesn't care.
-
24:09
So that item can be completely self contained without any knowledge of
-
24:12
who's doing the listening.
-
24:13
But can just broadcast those information, that information when it wants to.
-
24:19
All right. One major difference.
-
24:20
Just wanted to clarify this, that these events don't bubble.
-
24:23
Normally when you're dealing with events on components that
-
24:26
you've written yourself.
-
24:27
Maybe you're using a mix-in that will provide an event emitter,
-
24:31
something that you can use with your code.
-
24:33
They're not gonna bubble.
-
24:34
There's no document hierarchy for those to bubble through.
-
24:37
A notable exception, will be Angular.
-
24:40
If you're using their event system is built in a,
-
24:42
a structural way that reflects very similar to how the DOM works.
-
24:50
that, the other point of this is that delegation isn't something that,
-
24:53
technically delegation.
-
24:55
Well or key there because there's no bubbling.
-
24:57
So we use a message bus instead of delegation when we're
-
25:00
dealing with pure eventing and stuff like that in JavaScript.
-
25:04
All right, you're heard the term Pub/Sub or publish and subscribe.
-
25:07
Technically the observer pattern falls into this category.
-
25:11
But I, it does, I mean it clearly falls into this category.
-
25:14
I tend to use Pub/Sub when I'm talking about a separate message bus.
-
25:18
And event system is what I'm talking about.
-
25:20
You know what we might expect from a DOM event, or
-
25:22
from backbone events and so forth.
-
25:24
So it certainly publish and subscribe, you're triggering or
-
25:27
publishing an event and then other people are subscribing to it.
-
25:31
But if you hear it used a lot,
-
25:33
it may mean something bigger like a message bus as opposed to true eventing.
-
25:40
Okay, so here's where it's gonna be fun.
-
25:41
We're gonna jump into a, example application.
-
25:44
We're gonna build not actually build.
-
25:46
But we're gonna walk through it as if we're building it.
-
25:48
And kinda think through how we can try to integrate,
-
25:50
some of these eventing systems into our code, and see how that looks.
-
25:55
The nice thing is, with the observer pattern and a good API,
-
25:58
we can build our sites and applications as a collection of these discrete components.
-
26:03
That communicate with each other, using methods for input and events for output.
-
26:08
Of course we can use this pattern with non-visual components.
-
26:10
So those events don't have to be user interactions, it can be you know,
-
26:14
something from a web socket,
-
26:16
it can be one of your pieces of code is firing on, on a timer.
-
26:20
Any number of things can, can pose these events.
-
26:24
All right, so, why would we even bother, to add this to our code?
-
26:29
Well, components that are standalone, like I'm talking about,
-
26:32
I'm gonna walk through building, they're testable.
-
26:35
They don't require a huge infrastructure to test,
-
26:37
you load up that component, you test it's inputs.
-
26:40
Test the expected outputs, and you're good to go.
-
26:43
And then, of course, the components can be tested on their own, or
-
26:45
maybe tested how they integrate with your code.
-
26:48
But they're very easy to test.
-
26:49
They're completely separate.
-
26:50
And hopefully you guys,
-
26:52
when you're writing JavaScript are able to unit test that code.
-
26:55
So you have confidence in what you're doing.
-
26:57
It's reusable.
-
26:58
When a component knows intricacies about another component,
-
27:02
it has to always be used on tandem with that component,
-
27:04
unless you're building a lot of conditionals in there.
-
27:07
So our goal is that we have something that is able to be reusable, and be able
-
27:11
to maybe use throughout your application, not necessarily on another project.
-
27:15
It may be specific to your app.
-
27:17
But at least throughout the project without this brutal nature of oh no I've
-
27:21
gotta make sure I load that, and load this and then all of it will work correctly.
-
27:26
[SOUND] all right.
-
27:29
The example application we're gonna build which after I
-
27:32
gave a talk that included this application in Vienna and
-
27:34
someone told me apparently, all illustrations are of video searching apps.
-
27:38
I didn't know that, but it actually works really well for what we're doing.
-
27:41
so, we're going to work through building this example video app.
-
27:47
So you see we have a Search bar at the top,
-
27:50
it's gonna show us movies at the bottom.
-
27:53
Maybe you know things are gonna open our theater, whatever.
-
27:56
It's gonna show us that response.
-
27:58
And then when we click on one it's gonna show us a Details tab,
-
28:01
which is gonna look like this.
-
28:04
So it's, you know a very simple interface.
-
28:05
We've got the Search field, the Search button and then our Result view and
-
28:09
our Details view.
-
28:11
All right, lots of code.
-
28:12
We're gonna step through it, don't be overwhelmed.
-
28:15
Walk through it one by one here.
-
28:17
But this is the code.
-
28:18
This is just for the searching, that I'm showing here.
-
28:20
And then we'd, we'd have code for the other part as well.
-
28:23
But so here, don't take this and go write an app that looks like this.
-
28:26
I'm starting somewhere that would be where there's no components.
-
28:29
There's nothing that's been separated out.
-
28:30
It's just straight jQuery code.
-
28:32
So this is where we're gonna start and then we'll move toward a separate
-
28:36
components that admit events and accept inputs.
-
28:40
So, as we step through this.
-
28:43
Here's our search form.
-
28:44
We're just listening for the submit.
-
28:46
Please never bind to the click event on a submit button.
-
28:49
You can either bind to submit event on the form.
-
28:51
That way the user can't accidentally submit the form.
-
28:56
Then we're gonna prevent defaults so that form doesn't send to the server.
-
29:00
We're gonna look up our query variable which is the search field.
-
29:03
If there's not a query we'll return it.
-
29:05
We won't do anything else so
-
29:07
they can't hit Enter in an empty field and have it ex, execute a search.
-
29:11
Finally we're going to make our AJAX request to our endpoint here.
-
29:15
Passing the query.
-
29:17
And wait and handle the results all in this method.
-
29:20
And that's where, just assume between those three dots that you're seeing here,
-
29:23
that there's a magical templating library that's taking care of RenderMan's force.
-
29:27
I don't wanna get bogged down in the details here.
-
29:30
And then we're finally setting up for HTML and, and to, to stage point from earlier.
-
29:35
Hopefully at this point this is trusted HTML from our server,
-
29:38
not user input each time.
-
29:41
Okay. So just to reiterate what happened in
-
29:43
a visual way, clicking the Search button gets the search query,
-
29:46
ensures that one was provided, executes the AJAX request, and renders the results.
-
29:52
That's a lot of stuff for a Search button to do.
-
29:55
And then clicking on an individual movie fetches data over Ajax,
-
29:59
renders the results, and then changes the tab to show the details.
-
30:05
All right, so obviously there are some issues here.
-
30:06
We can't test any of these pieces on their own.
-
30:09
Just to test the display of results, we'd have to fill in the search box and
-
30:13
trigger a click, on the Search button.
-
30:15
That's the only way we could do it.
-
30:16
We'd have to simulate a click on the Search button,
-
30:18
after having filled in the search box and
-
30:20
then we can see if the events rendered correctly, the movies rendered correctly.
-
30:25
We'd have to have a live endpoint running, which isn't that's an issue, but
-
30:28
the mock, we can mock a request.
-
30:29
We certainly do that quite a bit in front end development.
-
30:33
And then that's all just so we can verify that the rendering function works,
-
30:35
cuz it's nested so deeply into that code.
-
30:39
And then to change our Details view maybe from a tab to a dialogue we'd have to
-
30:43
change the event handler for an individual movie.
-
30:45
I didn't show this code.
-
30:47
You can imagine,
-
30:47
if we have code that looked like that the rest of it would be similar.
-
30:51
And clicking on an event is the thing that shows the details.
-
30:54
Now the event for that click is responsible for
-
30:56
rendering the details which, as you say that loud you start to
-
30:59
realize this doesn't sound like a well architected piece of application.
-
31:04
All right.
-
31:05
Does it work?
-
31:06
Yeah. Theoretically it would work just fine.
-
31:08
As long as it didn't get anymore complicated it would work.
-
31:11
Can we make it better?
-
31:11
Yes. And that's what we're gonna talk
-
31:13
through in the next little bit.
-
31:16
All right so the first thing you'll wanna do is take a step back,
-
31:18
instead of just saying I need to wire this to this and do this to that, we wanna take
-
31:22
a step back and identify what components, what pieces are we going to need.
-
31:26
I do wanna call out,
-
31:27
I'm not talking specifically about the spec web components.
-
31:30
I'm talking about just modularizing your code into reusable chunks.
-
31:34
So just, I don't wanna get anyone caught up in the terms.
-
31:38
All right. So if we look at this,
-
31:40
we see there's this area that's related to Search Query and Execution.
-
31:44
That's one segment up at the top.
-
31:46
We have our Search Results for you.
-
31:49
We have a Details view, which I don't show on this screen again, but
-
31:51
you saw that earlier.
-
31:52
We have the Details view.
-
31:53
And then we have Tabs, those are just great,
-
31:55
they do one thing, which is switch between views.
-
31:58
And we've seen two different ways to do tabs today,
-
32:00
so you should have, plenty of ways to do that.
-
32:03
And then you may or
-
32:04
may not have a movie object depending on how complicated your app might be.
-
32:07
But we could potentially have something that's called a [INAUDIBLE].
-
32:11
Excuse me.
-
32:13
All right, so when we're at these components, we want them to have to obey,
-
32:17
a focused use which is called the single responsibility principle.
-
32:20
Where right now,
-
32:21
in our previous version here that we just showed, the Search button.
-
32:26
Are the form being submitted is responsible for getting the results and
-
32:29
rendering them and validating the query that was entered.
-
32:33
So it's doing a lot of things.
-
32:35
Nothing's related directly to what would visually appear to be its use.
-
32:39
And then we wanna make sure that whatever we build has a clear public API.
-
32:43
That's in the, it's not public in the sense of users using your app but public,
-
32:46
in the sense that the rest of your app can depend, that it has these methods and
-
32:50
fires these events.
-
32:54
So let's talk about that search component.
-
32:55
That's the one at the very top.
-
32:56
What is its purpose?
-
32:58
When we really sit down and look at it, what's its purpose?
-
33:00
Well, allow the use to enter a search term.
-
33:03
Make sense?
-
33:04
Start the search.
-
33:05
Okay it's gonna trigger the search.
-
33:06
Ad it wants to continue to display the search term, once the search is rendered.
-
33:11
So we don't wanna hit search and have the term go away.
-
33:13
We want it to be kind of at most like a header, and
-
33:15
show us what the results are for one that we're currently looking at.
-
33:19
The thing it is not supposed to do, it is is not supposed to request data, or
-
33:23
render results.
-
33:24
That doesn't make sense for a search component to do.
-
33:27
It would be like an input making AJAX request in html.
-
33:31
We just wouldn't expect that to happen.
-
33:33
So our search component, that's not part of it's core purpose.
-
33:38
So we see it's going to output, we're,
-
33:40
talk about the output first it's gonna publish a search requested event.
-
33:44
And this can provide the term hobbit.
-
33:46
This is our, not always hobbit, of course it will be whatever it searched for,
-
33:50
but, hobbit's a good term.
-
33:51
So we'll have search requested, and
-
33:53
then the term will be whatever they searched for.
-
33:55
That's when a form's submitted, and when it's filled in.
-
33:58
So, we wouldn't do anything if the form was empty or just blank space.
-
34:02
The input, it's gonna take two methods.
-
34:04
One is set search term.
-
34:05
That will set what should show up in that box, but it won't trigger anything.
-
34:10
And then trigger search.
-
34:11
And this allows us to programmatically,
-
34:14
cause a search to happen without filling in the input and clicking the button.
-
34:19
Right now we're using methods through an API.
-
34:20
It's a lot cleaner, and doesn't require our,
-
34:23
the rest of our app to understand how the internals of this component are running.
-
34:28
Okay, here's the internal responsibility.
-
34:30
That was the external responsibility.
-
34:32
That's what you have to maintain for the rest of your app.
-
34:34
Internally, we'll listen to the submit event on the form, and run triggerSearch.
-
34:38
Now we have a method to run, to do this.
-
34:41
The triggerSearch method can retrieve the value of the search box.
-
34:44
Make sure a value has been entered.
-
34:46
And then publish that message force.
-
34:51
Okay, so taking a step back, how do we publish the event?
-
34:56
Being somewhat, this talk being somewhat framework agnostic I'll show you how to do
-
34:59
it in Backbone and then I'll mention how to do it in angular.
-
35:02
You can of course do it a million different ways.
-
35:05
But at the end of the day you need a library that has an event emitter built
-
35:08
into it or you need to load an event emitter,
-
35:10
and I'll show you one called monologue in a minute.
-
35:13
So you need something that has an on, an off and a trigger or emit method so
-
35:17
you can subscribe to events, unsubscribe from events and
-
35:20
then have a way to trigger or emit those events.
-
35:23
So there's plenty of libraries out there that do this.
-
35:25
Some of them have it built in.
-
35:28
If we looked at it in Backbone I'm not showing you half of the code here but
-
35:32
you can imagine we have a backbone view,
-
35:34
there's a render method somewhere else that's doing stuff, and in here.
-
35:39
You're going to, when we click Trigger Search and
-
35:41
we, when we make that happen, we're going to grab our current context, which is
-
35:46
going to be the root of the control, find that search field, grab its value.
-
35:50
If you're working with a model back view,
-
35:52
you're just probably gonna get that off the model.
-
35:55
We're gonna use jQuery's trim method to go ahead and
-
35:58
just remove any blank space that might be in there.
-
36:00
Make sure a term exists.
-
36:01
Which is this line here.
-
36:03
And as long as there's a term that exists, we're gonna allow the code to continue.
-
36:06
And we're gonna call this.trigger, search.started.
-
36:10
And actually I meant to say requested here.
-
36:12
But search.requested with our term.
-
36:14
And that's how that will output Let me do this so
-
36:19
I don't have to remember to do it later, a second.
-
36:22
One of the features of the Blazen is this, requested.
-
36:25
There we go. See?
-
36:26
It's like I never even made the mistake.
-
36:28
All right so this is search requested.
-
36:31
And this will trigger that for our element.
-
36:33
If you're using something like Angular I know a lot of people are using this.
-
36:37
It has a concept of a message, or excuse me, an event emitter.
-
36:41
You can use emit on your scope which will publish an event up the scope hierarchy,
-
36:45
the whole way up to the, to the root.
-
36:47
You can use, if it's not stopped, somewhere along the way, broadcast,
-
36:50
which will take the context element and go the whole way down to any bits children.
-
36:54
Which is something that we don't really have a parallel with in the DOM
-
36:57
eventing we're looking at.
-
36:59
At then of course, if you really needed the root level and
-
37:01
everything you could go to rootScope and then broadcast it there.
-
37:05
If this doesn't make sense to people who don't use Angular, it's okay.
-
37:08
It's just meant for those who do use it to know how, how to where to find it.
-
37:12
And I have a link to the documentation, so
-
37:13
you could read more about the event emitter that's there.
-
37:15
And I didn't do it.
-
37:18
And I wanna thank Jonathan Creamer for helping me.
-
37:20
God, help with that aspect and making sure that that is useful for everyone.
-
37:24
A monologue is a completely standalone event emitter.
-
37:28
I use Postal, which is for a message bus.
-
37:30
And Monologue is kind of a companion library to that.
-
37:34
So it's something you could check out as, as well.
-
37:38
All right. The important thing.
-
37:39
Take the step back.
-
37:41
We've looked at everything.
-
37:41
We have these components.
-
37:42
The important thing is that you honor that external contract.
-
37:45
The events that you're gonna publish.
-
37:47
And the API that you have.
-
37:48
As long as you keep that consistent, you can change the internals of
-
37:52
that control 50 million times in your app and you won't break anything as long as
-
37:56
you publish the events when you should and you accept those two methods we mentioned.
-
38:00
So, now you can't look at it anymore as a search box and a button.
-
38:03
It's the search control.
-
38:05
It's like, the table.
-
38:06
It's like the form.
-
38:07
It's, it's a single component that does a single use, and here's its API.
-
38:11
You have to honor that or else this all breaks down.
-
38:14
Soon as your search box and button reaches out to a parent element and
-
38:17
calls some method it knows about, you've broken the, the reusability and
-
38:21
testability of your code.
-
38:23
So make sure you honor that external contract.
-
38:27
All right. We're not gonna go that deep into each of
-
38:29
the other objects, but we can expect that our
-
38:31
Results view would accept a method that's called updateResults.
-
38:34
And that's gonna pass in the data from the server.
-
38:37
That will, it will then be in, responsible for rendering.
-
38:40
Will have an output of movie selected, so when you do click on a movie,
-
38:43
the Results view is the thing that's gonna output that event for
-
38:46
you, knowing that a movie's selected.
-
38:47
You don't have to bind an event for your app, directly to that movie.
-
38:52
We'll have tabs that will maybe accept an input of changeTab, and
-
38:55
you can give it the idea the tab you wanna go to.
-
38:57
And it's gonna output when the tab.changed.
-
39:01
The code that I'm gonna show, the pseudocode, doesn't use this one, but
-
39:04
this makes sense.
-
39:05
You'd wanna know when the tab changes.
-
39:07
And then final the detail view is just going to have one piece which is
-
39:10
show movie.
-
39:11
We say show movie and give it some code.
-
39:12
It's gonna know how to run through that and properly display it.
-
39:16
So now we've kind of split this up into these components.
-
39:19
We have a tabs, search control, our results and details.
-
39:23
We have these separate components now that we can use.
-
39:25
And this again using any number of object patterns here.
-
39:28
As long as you have an event emitter, you'll be able to do this.
-
39:33
All right, here's what our updated code looks like.
-
39:36
It's a lot easier to understand.
-
39:37
We remember, keep in mind these variable names, tabs, search, results, and
-
39:42
details, cuz this is kind of a continuation of that code.
-
39:45
So if we listen using event emitter to the search requested event We can wait for
-
39:51
that, and under the data its gonna have that permit we need.
-
39:54
And so we can make an AJAX request with that term.
-
39:58
When Ajax results come back, we can tell our results to update the results.
-
40:03
We don't care about how it's going to do that.
-
40:06
We don't have the rendering logic in here like we had before.
-
40:09
We're just going to put this in here.
-
40:10
We could go even further and do something like amplify request using AmplifyJS to
-
40:15
abstract out that Ajax request.
-
40:18
There's a lot of things you could do, but this cleans it up considerably.
-
40:21
Now when a search is requested it will change the tab to results,
-
40:23
which that's the one that's showing.
-
40:25
So if details was showing before it will show the results, make our Ajax request,
-
40:29
and then render those, render that data.
-
40:31
Now the same thing for movies selected.
-
40:35
When a movie's clicked we're gonna change the tab to details, make a request for
-
40:38
the movie and then you show the movie.
-
40:41
But if you sit someone down here and they look at
-
40:43
this they don't even have to understand what your dom structure looks like.
-
40:46
They can read, read this code, understand what's going on and then when you need to
-
40:51
make a change to the detailed view, you're not looking at the event handler for
-
40:54
movie, you're looking to the detail component.
-
40:57
That makes it a lot easier to find where things are going wrong and
-
41:00
where you need to fix it.
-
41:00
All right, this is better, right?
-
41:04
There's probably more code now than there was before but
-
41:08
does it meet our needs of being maintainable?
-
41:10
Yeah, you know where stuff is and it, it has a single responsibility.
-
41:14
Is it testable?
-
41:14
Absolutely, these components are far more testable than they were before.
-
41:18
And it's even reusable.
-
41:19
We can use that search control to do other things throughout the app, and
-
41:23
we could make it even more generic if we wanted to.
-
41:26
So it gives us a lot of benefit for what we're working on.
-
41:30
All right.
-
41:31
Talking about earlier, with the dom event, event delegation is, is great and
-
41:35
you can do all this cool stuff with it.
-
41:37
Can you use it with event emitters?
-
41:40
Well, in. Let's talk about what delegation gets us.
-
41:42
Cuz I say in a general sense, yes, but I told you earlier you couldn't.
-
41:45
So let's clarify what I mean.
-
41:47
Delegation gave us the ability to listen for
-
41:49
events without a direct reference to the element.
-
41:52
So we could add, add the event handler higher up on the dom, and
-
41:55
then the event would bubble up.
-
41:57
And it also allowed us to filter what we wanted to do based on
-
42:00
characteristics of that element, and we can do that with delegation.
-
42:04
When we're talking about eventing and
-
42:05
doing this apart from the DOM we use a thing called a message bus.
-
42:10
And there's a number of different names for
-
42:11
this event aggregator, mediator, message broker.
-
42:15
They all have the same concept though,
-
42:16
which is they introduced a shared object that is used to
-
42:20
communicate between components instead of requiring a direct reference.
-
42:24
Because right now our
-
42:25
app required a direct reference to each of these components to wire them up.
-
42:28
Which is fine.
-
42:29
It's a great pattern.
-
42:30
So our app we'll come back to that slide in a second.
-
42:33
But our app looks like this.
-
42:34
There's the app then it has referenced each of the controls.
-
42:39
The message bus works like a shared observable object.
-
42:42
So it's going to have events that are emitted.
-
42:45
We call those topics generally, or messages rather, and
-
42:48
then the event name we call a topic.
-
42:52
The methods for these are often different, so unsubscribe, subscribe, and
-
42:55
publish, that's where you get kind of more of the pub/sub terms.
-
42:59
Though they certainly can still have on, off, and
-
43:01
emit as the API depending on the library that you're using.
-
43:03
But it takes our app from this and makes it look like this.
-
43:09
So, instead of everything, the app having a reference to everything else,
-
43:14
these items will have reference to that global message bus.
-
43:17
And then could publish something on the bus that then the app could pick up and
-
43:21
go back the other direction.
-
43:23
Again I mentioned we wouldn't go too deep into this but there are some differences.
-
43:27
One thing is that your methods need to support now,
-
43:32
your objects rather need to support being called via an action message.
-
43:35
When we're dealing with events on objects traditionally those aren't actions.
-
43:38
Those aren't going to say things like start search or, or
-
43:42
some things very specific like that.
-
43:44
They're going to say this happens.
-
43:45
So search requested or form submitted.
-
43:48
Anything like that.
-
43:49
That's kind of an event.
-
43:51
Message bus you now need to be able to process inputs through the same
-
43:55
channel that you're sending the outputs.
-
43:56
And that's where you get action messages like search trigger and
-
44:00
maybe that would handle that method trigger search.
-
44:05
So the action message, it needs to be kind of wired in and there's ways to do this.
-
44:10
But you, it's one difference between a true event emitter and
-
44:12
then something that can handle inputs through that same event emitter.
-
44:17
Two libraries you can check out.
-
44:18
One the AMPLIFYJS, the one appendTo's written.
-
44:21
And that one has a very simple pub/sub.
-
44:23
It kind of gives you that shared object.
-
44:25
There's someone, I can't remember the guy's name,
-
44:28
he tweets a one that fits in tweet.
-
44:30
There's another very short one that's used by quite of few people.
-
44:34
For message buses, things that are shared that way.
-
44:37
Postal.js is the one that at appendTo we traditionally use.
-
44:39
It gives you a lot of power.
-
44:41
It has it basically matches if you've ever used RabbitMQ or
-
44:44
something like that on the server, it has the same wildcard matching support.
-
44:48
It gives you a lot of power when you're building complex apps to put
-
44:51
those things together.
-
44:54
So those are some libraries that you can use.
-
44:56
All right, we are coming to the end of a large number of slides.
-
44:59
I just want to reiterate if some of this was new information.
-
45:03
Kind of putting it all together, what would be a good next step?
-
45:07
If you don't group your code in components at all right now and
-
45:09
you just kind of have that initial example where you have a lot of jQuery code that
-
45:13
maybe runs in document.ready and tries to wire everything up.
-
45:16
Just start looking at your application based on components.
-
45:19
Start breaking it up into reusable components, making it easier to maintain,
-
45:23
easier to test.
-
45:24
That's a great next step.
-
45:26
Don't worry about the message bus.
-
45:28
Maybe not even about the event emitters yet.
-
45:30
Just try to break your code up into more usable pieces.
-
45:34
If you already are doing components, but really aren't using those events, and
-
45:37
you're kind of one component knows the internals of another component and
-
45:40
they're interacting with each other that way.
-
45:42
A great next step would be to get an event emitter in there,
-
45:45
maybe you already have one, you're just not using it.
-
45:47
Get that in place and start using that to kind of separate the, the logic.
-
45:52
So this child component doesn't have to know about the internals of the parent.
-
45:56
That's a great next step.
-
45:56
And if you're already doing those two things you have components and
-
45:59
they're of using event admitting but
-
46:01
you're not using a message about something that's more global.
-
46:04
Look at your app.
-
46:04
See is it complex enough that it makes sense that I want to
-
46:07
have these as truly separate components.
-
46:10
The tool we're using here, Blazen, it uses a very complex message bus.
-
46:13
And part of that is we're forwarding information through, and
-
46:17
back out of an iframe.
-
46:18
And, it's filtering that for content, making sure the stuff correct.
-
46:21
But we're running everything in a sandbox, but
-
46:23
allowing people that are logged in to make edits from the parent page.
-
46:27
So, because we go through a message bus, and everything serializable,
-
46:31
we can send it directly to the other frame and back if you go to the server and back.
-
46:35
There's a number of things you can do when you use a message bust that you
-
46:38
just simply can't do another way or, or as cleanly another way.
-
46:42
There's a lot of really cool stuff we can do, but
-
46:45
that's it that's what I had for you.
-
46:47
I hope that there's some questions that I can answer.
-
46:49
I've actually got done on time which I was worried about given the number of slides I
-
46:53
was going through.
-
46:54
Any questions that I could answer?
You need to sign up for Treehouse in order to download course files.
Sign up