In this video, you built the smallest version of Alex’s packing list that runs end to end, in a new packing-list folder inside the folder you serve with Live Preview, one file at a time.
Key Points
- Add one file at a time, and check the page after each one. If you wrote every file first and the list stayed empty, the problem could be in any of them. Each check proves one more link, so a failure points at the file you added last.
- The first link: the page runs its script.
index.htmlholds a heading, an empty heading for the trip’s name, an empty list for its items, and a<script type="module">tag formain.js. Opened with Live Preview athttp://127.0.0.1:3000/packing-list/index.html, the page shows the heading, and the Console showsPacking list loaded. - The second link: the data reaches the script.
state.jsexports one trip, Beach Week, with four items. Each item already has the shape from your plan: a name, a category and a packed state, though the page shows only the names for now. Whenmain.jsimports the trip and logs its name and its number of items, the Console showsBeach Week 4. - The last link: the data reaches the page.
render.jsexportsrender(), which writes the trip’s name into the empty heading, empties the list, and builds one list item per item from the data.main.jsimportsrender()and calls it once. The page shows Beach Week with Swimsuit, Sunscreen, Passport and Phone charger. - Change the data, and the page follows. Add a toothbrush to
state.jsand save. You don’t touch any other file, and the toothbrush appears at the end of the list. Saving reloaded the page, somain.jscalledrender()again, andrender()drew the list from the new data. - This is a walking skeleton. Each file has one job:
state.jsholds the data,render.jsdraws it andmain.jsstarts things up. Every link between the files is in place with almost nothing on it, and the skeleton runs end to end. As a rule, build the thinnest version that runs end to end first, and check the page after each file you add. - It’s a toy for now. It has one trip, no categories on screen, nothing to tick off and nothing saved. Each one arrives later as a step on top of these files.
The Code
These are the four files in packing-list, as the video left them before the toothbrush:
packing-list/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Packing List</title>
</head>
<body>
<h1>Alex's Packing List</h1>
<h2 id="trip-name"></h2>
<ul id="items"></ul>
<script type="module" src="main.js"></script>
</body>
</html>
state.js holds the data, one trip with four items:
packing-list/state.js
export const trip = {
name: "Beach Week",
items: [
{ name: "Swimsuit", category: "Clothes", packed: false },
{ name: "Sunscreen", category: "Toiletries", packed: false },
{ name: "Passport", category: "Documents", packed: false },
{ name: "Phone charger", category: "Gear", packed: false },
],
};
After the toothbrush step, items ends with { name: "Toothbrush", category: "Toiletries", packed: false },. Keep it, since the later steps start from these five items.
render.js draws the trip into the page:
packing-list/render.js
import { trip } from "./state.js";
const tripName = document.querySelector("#trip-name");
const itemList = document.querySelector("#items");
export function render() {
tripName.textContent = trip.name;
itemList.replaceChildren();
for (const item of trip.items) {
const row = document.createElement("li");
row.textContent = item.name;
itemList.append(row);
}
}
main.js starts things up:
packing-list/main.js
import { render } from "./render.js";
render();
On the way there, main.js held one log line first, console.log("Packing list loaded");, then imported the trip to log it:
packing-list/main.js
import { trip } from "./state.js";
console.log(trip.name, trip.items.length);
Next, in Exercise: List One Trip’s Items, you’ll rebuild this list in its own starter and add a form, so Alex can add what they forgot. Then you’ll copy the code into this folder.