Merge pull request #126 from metosin/frontendrouter

reitit.frontend/router
This commit is contained in:
Juho Teperi 2018-08-23 10:07:09 +03:00 committed by GitHub
commit adfba578ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 14 deletions

View file

@ -1,6 +1,5 @@
(ns frontend.core
(:require [reagent.core :as r]
[reitit.core :as re]
[reitit.frontend :as rf]
[reitit.frontend.easy :as rfe]
[reitit.frontend.controllers :as rfc]
@ -45,7 +44,7 @@
(apply js/console.log params)))
(def routes
(re/router
(rf/router
["/"
[""
{:name ::frontpage
@ -72,8 +71,7 @@
(js/console.log "start" "item controller" (:id params)))
:stop (fn [params]
(js/console.log "stop" "item controller" (:id params)))}]}]]]
{:compile rc/compile-request-coercers
:data {:controllers [{:start (log-fn "start" "root-controller")
{:data {:controllers [{:start (log-fn "start" "root-controller")
:stop (log-fn "stop" "root controller")}]
:coercion rsc/coercion}}))

View file

@ -1,6 +1,5 @@
(ns frontend.core
(:require [reagent.core :as r]
[reitit.core :as re]
[reitit.frontend :as rf]
[reitit.frontend.easy :as rfe]
[reitit.coercion :as rc]
@ -53,7 +52,7 @@
[:pre (with-out-str (fedn/pprint @match))]])
(def routes
(re/router
(rf/router
["/"
[""
{:name ::frontpage
@ -66,8 +65,7 @@
:view item-page
:parameters {:path {:id s/Int}
:query {(s/optional-key :foo) s/Keyword}}}]]
{:compile rc/compile-request-coercers
:data {:coercion rsc/coercion}}))
{:data {:coercion rsc/coercion}}))
(defn init! []
(rfe/start! routes

View file

@ -1,8 +1,9 @@
(ns reitit.frontend
""
(:require [reitit.core :as reitit]
[clojure.set :as set]
[reitit.coercion :as coercion])
(:require [clojure.set :as set]
[reitit.coercion :as coercion]
[reitit.coercion :as rc]
[reitit.core :as r])
(:import goog.Uri))
(defn query-params
@ -19,7 +20,7 @@
coerced parameters. Return nil if no match found."
[router path]
(let [uri (.parse Uri path)]
(if-let [match (reitit/match-by-path router (.getPath uri))]
(if-let [match (r/match-by-path router (.getPath uri))]
(let [q (query-params uri)
;; Return uncoerced values if coercion is not enabled - so
;; that tha parameters are always accessible from same property.
@ -32,7 +33,15 @@
([router name]
(match-by-name router name {}))
([router name path-params]
(reitit/match-by-name router name path-params)))
(r/match-by-name router name path-params)))
(defn router
"Create a `reitit.core.router` from raw route data and optionally an options map.
Enables request coercion. See [[reitit.core.router]] for details on options."
([raw-routes]
(router raw-routes {}))
([raw-routes opts]
(r/router raw-routes (merge {:compile rc/compile-request-coercers} opts))))
(defn match-by-name!
"Logs problems using console.warn"
@ -40,7 +49,7 @@
(match-by-name! router name {}))
([router name path-params]
(if-let [match (match-by-name router name path-params)]
(if (reitit/partial-match? match)
(if (r/partial-match? match)
(if (every? #(contains? path-params %) (:required match))
match
(let [defined (-> path-params keys set)