Give Each Record a Stable Id

At the end of the last video, every handler on Priya’s page followed the same steps: find the record by the ID its button carries, change players, and call render(). Each of those steps trusts that the ID points at the right guest.

In this lesson, you’ll give Priya a view of the guests she’s still waiting for, drawn by render() from the same records. Then you’ll use that view to see how the ID you gave late guests in the last lesson can point at the wrong person, and you’ll replace it with one that can’t repeat.

Show Only Who’s Still to Come

As the evening fills up, Priya cares less about who’s already here and more about who she’s still waiting for. That view needs no new records. The guests and their arrived fields are already in players. What’s new is a setting: a fact about how to show the data, rather than about the guests.

First, give the view a button. In scratch/index.html, add it right after the <h2>Who's Coming</h2> heading, outside the list:

Language: HTML Filename: scratch/index.html
<button id="still-to-come">Still to come</button>

In main.js, find the button and keep the setting in a variable under the const bar line at the top. Its value changes on every click, so declare the variable with let:

Language: JavaScript Filename: scratch/main.js
const toggleButton = document.querySelector("#still-to-come");
let stillToCome = false;

Now render() decides which rows to draw. In its loop, wrap the playerList.append() line in an if statement, so the loop starts like this:

Language: JavaScript Filename: scratch/main.js
  for (const player of players) {
    if (stillToCome === false || player.arrived === false) {
      playerList.append(drawRow(player));
    }

Read the condition as “draw the row when Priya wants everyone, or when this guest hasn’t arrived.” The two totals below it stay outside the if, so Arrived and the bar still count every guest, whichever rows are drawn.

The button should also say what a click on it will do. At the end of render(), after the bar’s if, add:

Language: JavaScript Filename: scratch/main.js
  if (stillToCome) {
    toggleButton.textContent = "Show everyone";
  } else {
    toggleButton.textContent = "Still to come";
  }

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.