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
MediumKeep 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>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Routing and State
4Query State
Reading Query ValuesCommitting Search QueriesPreserving Other FiltersNormalizing Query NumbersRecap: Catalog Filters2Navigation Actions
Navigation from HandlersReplacing HistoryHistory TravelCurrent Navigation LinksRecap: Save and Review5Nested Layouts
Parent and Child OutletsDefault Child ViewsRelative Child PathsAncestor Navigation StateRecap: Library Workspace8Store Design
Setup Store StructurePatching Related StateResetting Store StateComposing Related StoresRecap: Trip Pack11Workshop Planner Project
Planner RoutesShared Reservations3Dynamic Routes
Path ParametersReactive Route SelectionRoute Parameters as PropsMissing Route RecordsRecap: Equipment Routes6Route Policies
Redirecting Old PathsUnmatched PathsRoute MetadataCancelling NavigationRecap: Club Destinations9Shared Routed State
State Across ViewsRoute-Selected Store RecordsQuery and Store FiltersDraft and Commit OwnershipRecap: Reading Queue