tape.router
Integrates the reitit router via a
tape.module
module, and provides a few handy ways to navigate.
The router is optional and can be left out in apps that don’t need one.
You must be familiar with reitit,
tape.module
and tape.mvc
before proceeding.
In config.edn
add :tape.router/module
and define routes:
{:tape.profile/base
{:tape.router/routes [["/hi/:say" :sample.app.greet.controller/hi]]}
;; ...
:tape.router/module nil}
Routes are collected from the system map from keys derived from ::mvc/routes
.
Note: (mvc/defm ::module)
collects vars annotated with ^::mvc/routes
adds
them to the module map and derives them, so you can add routes in your
controllers:
(def ^{::mvc/reg ::mvc/routes} routes
["/people"
["" ::index]
["/show" ::show]])
;; ...
(c/defm ::module)
A route resolves to a namespaced keyword that gets dispatched on route match:
[::greet.c/hi parameters]
. The parameters
has a :path
map with params
matched in the path, and a :query
map with params matched in the query. The
keyword is also the name of the route.
(ns sample.app.my-ns
(:require [tape.router :as router :include-macros true]
[sample.app.greet.controller :as greet.c]))
To navigate we use the route name and a params map that fills the route params slots; we have the following utilities:
-
(router/href* [::greet.c/hi {:say "Hi!"}])
returns the path::greet.c/hi
with path params filled from the provided map{:say "Hi!"}
. Is used in hiccup anchor hrefs:[:a {:href (router/href* …)}]
. -
(router/navigate* [::greet.c/hi {:say "Hi!"}])
to navigate to a route, used in DOM event handlers (on-click etc). -
[::router/navigate [::greet.c/hi {:say "Hi!"}]]
a Re-Frame event to that can be dispatched to navigate to a route. -
{::router/navigate [::greet.c/hi {:say "Hi!"}]}
a Re-Frame effect to that results in the browser navigating to a route.
Ergonomic API uses macros with symbols of controller event handlers. Such symbols can be navigated via IDE "jump to definition". The macros are macroexpanded in the API above, so there’s no performance penalty at runtime.
-
(router/href [greet.c/hi {:say "Hi!"}])
-
(router/navigate [greet.c/hi {:say "Hi!"}])