This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Design Pattern: Simple Mediator

1:23 Angular 1.x lesson by

In this video Brett shows us how to create a simple mediator for cleanly separating concerns within your application. This is the first in a series that will build a robust, fully tested mediator for events in your AngularJS application.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

In this video Brett shows us how to create a simple mediator for cleanly separating concerns within your application. This is the first in a series that will build a robust, fully tested mediator for events in your AngularJS application.

Avatar
Samuel

Yeay, vim!

But i think you can bump up the font size just a bit, for the next video.

I'm also a bit skeptical to the placement of code with side-effects in the constructor of both objects.

In reply to egghead.io
Avatar
Nicola

I'm sorry, could you give us the html file please? Thank you in advance.
I really appreciate your tutorials
Cheers,
Nicola

Avatar
Andreas

Show us the html code plz.

In reply to egghead.io
Avatar
seongkwang

How can I see videos which belong to the series?

Brett Shollenberger: Today we're going to talk about the mediator pattern which works really great in JavaScript because it's an event driven language. We're going to use rootScope emit here to send out events and then we're going to listen for them in the run block. We're going to use rootScope on down in the run block to listen for the same event that we sent out to the system.

Whenever this event is heard it's going to fire this callback here which will pass in the event and the order that it heard. And then will, in that new callback, create an email and will send that email to the user. This is really great because it allows our order to know nothing about the fact that it creates emails.

But we don't want it to actually know about rootScope. We don't want to pass it in as a dependency because it has a lot of other functionality we don't want to give it access to. We're just going to wrap rootScope emit in this emit factory and we'll give it access to that instead.

Here we'll just pass in instead of rootScope emit and then down here we'll use emit the same way.

And so, when we save this out we can see that when we place new orders that we get those emails. Again, this is great because it allows our order to follow the single responsibility principle really, really well. The run block handles all the logic when it gets created.

Again, this is a really simple mediator. In later videos we'll show you more advanced logic. This should get you started.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?