Update, everything reacts
Records change with record.update(patch). It checks the new values against the schema, merges them in, and tells every view that read the record. Those views run data() again and re-render, whichever page or layout they sit in. Nothing has to pass the change along.
The shop's cards now have an Add button. @click={ add(album) } calls the add handler in the view's events with that card's album. (Chapter 5 covers events properly.) Playlist.pzl already lists every album with inPlaylist set, using findMany with a filter.
Your turn
- In
Shop.pzl, makeaddput the album in the playlist:
events = {
add: (album) => album.update({ inPlaylist: true }),
};
- In
layouts/Default.pzl, count the playlist indata(), so the header link shows it:
data(params, props) {
const playlist = this.ctx.store.findMany('album', { filter: (album) => album.inPlaylist });
return { count: playlist.length };
}
- Add a few albums in the preview and watch "Your playlist" in the header count them, then open the playlist.
The header is in the layout and the button is in the shop. They share no code: both read the same records, so both follow them.
The preview restarts the app whenever you edit a file, so the playlist empties when you type. A real app keeps its records across reloads with new PuzzleApp({ storage: localStorage }).