ECOSYSTEM / ROUTER

Ask before leaving

Keep the user on a page with unsaved changes, and know whether a navigation happened.

Ecosystem package janelkit-router Navigation between screens: page stacks that keep their state, parallel sections, results with await and transitions. pip install janelkit-router
ROUTER · PAGE 4 / 6~2 min read
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

A guard on the page

before_leave(fn), from the core, registers a question in a page or in any component inside it. When the page is about to be discarded (pop, back, replace, reset, Alt+Left) or its window is closed with the X, fn is called; returning False keeps the user where they are.

pop, back, replace and reset can be awaited: they return True if the page was left and False if a guard refused. The answer can arrive after a dialog.

src/App.janel
<script lang="python">
from janelkit_router import pages, Pages
from pages.Home import Home
nav = pages(Home)
</script>

<window title="Guards" width={420} height={260}>
  <Pages stack={nav} />
</window>
Copy
src/pages/Home.janel
<script lang="python">
from janelkit_router import navigation
from pages.Editor import Editor
nav = navigation()
</script>

<column padding={24} gap={12}>
  <text>Home page</text>
  <button on:click={nav.push(Editor)}>Edit</button>
</column>
Copy
src/pages/Editor.janel
<script lang="python">
from janelkit import state, before_leave
from janelkit_router import navigation

nav = navigation()
unsaved = state(True)
status = state("You have unsaved changes.")

@before_leave
def may_leave():
    return not unsaved.value

async def go_back():
    if not await nav.pop():
        status.value = "Leaving was refused: discard or save first."
</script>

<column padding={24} gap={12}>
  <text>{status}</text>
  <button on:click={go_back}>Back</button>
  <button on:click={unsaved = False}>Discard changes</button>
</column>
Copy

Complete example in 3 files · Save each one at the path shown above it.

How it works

  1. may_leave returns False while there are unsaved changes, so the first Back is refused.
  2. await nav.pop() returns False in that case, and the editor explains why.
  3. After Discard changes the guard returns True, and Back leaves.

What you should see

Edit, then Back: the editor stays and says Leaving was refused. Discard changes, then Back: Home page appears.

Ask with a dialog

The guard can be async, so it can ask the user. With janelkit-desktop, dialogs.confirm shows a native confirmation and returns True or False.

python
from janelkit import before_leave
from janelkit_desktop import dialogs

@before_leave
async def leave():
    return not changes.value or await dialogs.confirm("There are unsaved changes. Leave anyway?")
Copy

Learn moreDialogs ↗

Chain navigations

To navigate somewhere that requires leaving the current page first, wait for the answer. A detail page with changes may ask to stay, and then the second step must not happen.

python
async def go_to_card(project_id, card_id):
    if await nav.reset(Projects):        # a detail with changes may refuse
        nav.push(Project, ident=project_id, focus=card_id)
Copy

Common mistakes

  • Closing a window from code with close() does not ask. Only user navigation and the X do.
  • A guard is not a cleanup: if it refuses, the page must keep working. Release resources in on_destroy.

English documentation based on the JanelKit source. For additional details, browse the source docs ↗.