Skip to content

Make the example more concrete #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions page/events/introduction-to-custom-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ you have a lightbulb in a room in a house. The lightbulb is currently turned
on, and it's controlled by two three-way switches and a clapper:

```
<style>
.on { background-color: yellow; }
.off { background-color: black; color: white; }
</style>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let us handle this together with #535.


<div class="room" id="kitchen">
<div class="lightbulb on"></div>
<div class="switch"></div>
<div class="switch"></div>
<div class="clapper"></div>
<div class="lightbulb on">My room light</div>
<div class="switch">Switch 1</div>
<div class="switch">Switch 2</div>
<div class="clapper">Clapper switch</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR sorta overlaps with #535. Let's take care of it there.

</div>
```

Expand All @@ -55,10 +60,10 @@ $( ".switch, .clapper" ).click(function() {
});
```

With custom events, your code might look more like this:
With custom events, we can define our own events. Then your code might look more like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer we reword this a bit more and also stuck with the shorter changeState:

We can instead use a custom event, like changeState and our code would look more like this:


```
$( ".lightbulb" ).on( "changeState", function( e ) {
$( ".lightbulb" ).on( "changeTheLightState", function( e ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a better name now, committed in f9d0ab3.

var light = $( this );
if ( light.hasClass( "on" ) ) {
light.removeClass( "on" ).addClass( "off" );
Expand All @@ -68,11 +73,11 @@ $( ".lightbulb" ).on( "changeState", function( e ) {
});

$( ".switch, .clapper" ).click(function() {
$( this ).parent().find( ".lightbulb" ).trigger( "changeState" );
$( this ).parent().find( ".lightbulb" ).trigger( "changeTheLightState" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a better name now, committed in f9d0ab3.

});
```

This last bit of code is not that exciting, but something important has happened: we've moved the behavior of the lightbulb away from the switches and the clapper and to the lightbulb itself.
Here something important has happened: we've moved the behavior of the lightbulb away from the switches and the clapper and to the lightbulb itself.

Let's make our example a little more interesting. We'll add another room to our house, along with a master switch, as shown here:

Expand Down