Follow one search through the system
The finished app has four modules. model.js owns book decoding and pure state transitions. controller.js owns attempts, cancellation, and stale-result protection. service.js supplies a local catalogue with controlled failure scenarios. app.js finds the HTML elements, registers listeners, and renders state. The same source modules power the preview and downloadable project.
For one successful search, trace: form submit → controller.load → loading action → search dependency → decodeBooks → loaded action → render. For invalid data, decoding throws before a loaded action exists. For cancellation, generation changes before the old attempt rejects, so the obsolete catch cannot publish an error.
The demo's service error is a simulated HTTP-style failure, not an actual outgoing HTTP request. You can choose success, malformed data, or service error and a fast or slow response. This makes every required behavior reproducible without a third-party account, CORS setup, or internet connection.
Saved books live only in memory. Search results can be replaced without erasing the saved list. Saving again removes an existing id, so the action is a toggle. Unlike the beginner planner's one-way Complete action, this app deliberately supports both directions.
Read one module at a time. Do not memorize the combined HTML listing. Locate a rule in model, the moment it is called in controller, and the visible consequence in app. A working screen is not enough if you cannot identify which layer owns a failure.
Browser syntax to recognize
The scenario controls are HTML select elements: each option has a value, read through the select's .value property. The delay value is text, so the adapter converts it with Number. setAttribute("aria-busy", String(isLoading)) writes a text-valued accessibility attribute; it does not change the application status itself.
In app.js, void controller.load(query.value) starts the async action and deliberately discards its returned promise value. The controller handles expected request/decoding failures internally. An unexpected programming exception should still be investigated. window is the browser-provided page environment; its pagehide listener disposes the controller when the page is being discarded, while allowing a page retained in the browser’s back/forward cache to resume. This is the lifecycle distinction from Day 5.
Predict and trace
Write the output before running. Follow the values through each call; if your prediction differs, identify the first step that surprised you.
function toggleSaved(saved, book) {
if (saved.some(item => item.id === book.id)) {
return saved.filter(item => item.id !== book.id);
}
return [...saved, { ...book }];
}
const book = { id: "js", title: "Notes" };
const first = toggleSaved([], book);
const second = toggleSaved(first, book);
console.log(first.length);
console.log(second.length);Check your prediction
1
0Adding and removing are explicit value transformations. The first saved snapshot remains intact after the second toggle.
Read the status line as a function of state
render in app.js builds the status message with a chain of ? : expressions. Pulled out into a function, it depends on nothing but the state, so it can be traced without a page. Predict the three lines.
// The status line from app.js's render, as a function of state alone.
function statusText(state) {
return state.status === "loading"
? "Searching…"
: state.status === "error"
? state.error
: state.status === "ready"
? `${state.results.length} result(s). ${state.saved.length} saved.`
: "Enter a topic to search.";
}
const saved = [{ id: "web", title: "The Small Web" }];
console.log(statusText({ status: "idle", results: [], saved, error: "" }));
console.log(statusText({ status: "ready", results: [], saved, error: "" }));
console.log(statusText({ status: "error", results: [], saved, error: "HTTP 503: try again" }));Check your prediction
Enter a topic to search.
0 result(s). 1 saved.
HTTP 503: try againRead the chain top to bottom: each ? : is an if/else if, and the last value is the else. "idle" matches none of the named statuses, so it falls through to the final text. A ready search with no results still reports the saved count, because saved books are a separate part of the state that a search does not touch.
Run the finished app
Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.
Press Run webpage, search web, and save The Small Web. Then search testing. Inspect the two lists separately. All labels use textContent; a title containing angle brackets stays literal text.
Build it yourself
Summarise the saved list
Change the app so the Saved books heading always shows how many books are saved, and an empty saved list says so. Check this journey:
| Action | Heading | Saved list |
|---|---|---|
| Page loads | Saved books (0) | one row: Nothing saved yet. |
| Search web, save The Small Web | Saved books (1) | The Small Web with its Remove button |
| Remove it again | Saved books (0) | Nothing saved yet. |
| Click the "Nothing saved yet." row | unchanged | unchanged, and no error |
The lab below holds the whole app as one page: the HTML at the top, then model, controller, service and app code inside one <script type="module">. Your edits belong in the HTML and in the app code near the end.
Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.
Hint 1: the idea
Both pieces are projections of state.saved, so they belong in render, which already runs after every state change. The heading needs a way to be found from JavaScript, and the empty message is just one more li added when the list has nothing in it.
Hint 2: a first step
Give the heading an id, <h2 id="saved-heading">Saved books</h2>, find it next to const saved = ..., and set its textContent inside render just before the loop over state.saved. Run the page: the heading should read Saved books (0) before you click anything, because render also runs once at start-up.
Show one solution
Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.
The heading and the empty row are written in render, after saved.replaceChildren(), so they are rebuilt from state on every save, remove, search and cancel; nothing has to remember to update them. The empty message is a plain li without a button, so the delegated list listener's closest("button[data-id]") finds nothing and returns early: clicking it is harmless without any special case. Neither change touches model.js or controller.js, because no rule about saving changed, only what the page shows.
Explain before moving on
Which layer knows about HTTP? Which layer knows about DOM elements? Where does validation happen?
Completion is saved in this browser only. It does not measure mastery.