Menu
CoddyTech

Draft and Commit Ownership

Part of the Routing and State section of Coddy's Vue journey. Lesson 44 of 55.

An editor draft often belongs to the view while the committed value belongs to the store. Copy the current value into a local ref when the editor mounts, and call a store action only when the person commits. Leaving the view can then discard unfinished edits naturally.

const draft=ref(profile.bio);
function save(){profile.setBio(draft.value)}
function discard(){draft.value=profile.bio}

Binding the input directly to store state would make every keystroke visible elsewhere and remove the distinction between editing and saving. Validate and normalize at the commit boundary. Reopening the editor should begin from the latest committed value, not an old draft retained accidentally.

challenge icon

Challenge

Medium

Keep the note editor draft local. Entering Edit copies the current store note to input#draft; typing alone must not change shell p#note or editor p#committed. Commit trims a nonempty draft into the store. Blank commits do nothing. Leaving without committing discards the local draft, so reopening Edit reads the current committed note.

Keep the supplied route readout and bootstrap files.

Wrap committed note readouts p#note and p#committed in literal brackets so a stored note appears as [Bring pens], with no spaces inside the brackets beyond the note itself.

Try it yourself

<!doctype html><html><head><meta charset="UTF-8"><title>Routing practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Routing and State