-
Notifications
You must be signed in to change notification settings - Fork 0
Home
respo-router
is a router library for Respo. It's almost the same as router-as-view which was designed for React.js .
This module is not well tested in production yet. Be cautious if you want to use it.
- Demo http://router.respo.site/#/team/t12345/room/r1234?a=1&b=2
- Minimal example https://github.com/Respo/minimal-router/blob/master/src/minimal_router/main.cljs
respo-router
thinks that the router follow the MVC pattern too. Model of the router is stored in store
and its view is the address bar.
There are two ways the url may change:
- model updates, router changes
- by user interactions, also make sure model is updated
Store updates trigger rerendering. When (not= old-router new-router)
detected, address bar is rerendered.
For popstate
events, when respo-router.util.listener/listen!
is called, url change will be captured and will turn to a :router/route
action. Later on, it's a normal store update.
Address manipulation is achieved with (render-url! router dict mode)
, which will compare the router
to a cached version and decide how to update.
To create a router, we need a dict of rules:
(def dict
{"a" []
"b" ["x"]
"c" ["x" "y"]})
When it get a path like /a/b?c=d
, it tries to parse it like:
- gets path segments
["a" "b"]
, gets query{"c" "d"}
- looks up
"a"
in dict, gets path"a" []
, and["d"]
is left - looks up
"b"
in dict, gets"b" ["x"]
but params not matching - generates
{:name 404 :data ["b"]}
to say "not found"
So for /a/b/1
it's easier:
{:path [{:name "a", :data nil}
{:name "b", :data {"x" 1}}]
:query {}}
And for /
the router returns:
{:path [],
:query {}}
This project is in alpha stage, things may change.