Merge pull request #125 from metosin/frontend-fragment

Frontend fixes
This commit is contained in:
Juho Teperi 2018-08-23 10:12:46 +03:00 committed by GitHub
commit 2900e96337
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 40 additions and 9 deletions

View file

@ -49,4 +49,6 @@
:figwheel {:http-server-root "public" :figwheel {:http-server-root "public"
:server-port 3449 :server-port 3449
:nrepl-port 7002}) :nrepl-port 7002
;; Server index.html for all routes for HTML5 routing
:ring-handler backend.server/handler})

View file

@ -5,6 +5,6 @@
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>
<script src="js/app.js"></script> <script src="/js/app.js"></script>
</body> </body>
</html> </html>

View file

@ -0,0 +1,11 @@
(ns backend.server
(:require [clojure.java.io :as io]
[ring.util.response :as resp]
[ring.middleware.content-type :as content-type]))
(def handler
(-> (fn [request]
(or (resp/resource-response (:uri request) {:root "public"})
(-> (resp/resource-response "index.html" {:root "public"})
(resp/content-type "text/html"))))
content-type/wrap-content-type))

View file

@ -51,4 +51,6 @@
:figwheel {:http-server-root "public" :figwheel {:http-server-root "public"
:server-port 3449 :server-port 3449
:nrepl-port 7002}) :nrepl-port 7002
;; Server index.html for all routes for HTML5 routing
:ring-handler backend.server/handler})

View file

@ -5,6 +5,6 @@
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>
<script src="js/app.js"></script> <script src="/js/app.js"></script>
</body> </body>
</html> </html>

View file

@ -0,0 +1,11 @@
(ns backend.server
(:require [clojure.java.io :as io]
[ring.util.response :as resp]
[ring.middleware.content-type :as content-type]))
(def handler
(-> (fn [request]
(or (resp/resource-response (:uri request) {:root "public"})
(-> (resp/resource-response "index.html" {:root "public"})
(resp/content-type "text/html"))))
content-type/wrap-content-type))

View file

@ -22,9 +22,10 @@
(let [uri (.parse Uri path)] (let [uri (.parse Uri path)]
(if-let [match (r/match-by-path router (.getPath uri))] (if-let [match (r/match-by-path router (.getPath uri))]
(let [q (query-params uri) (let [q (query-params uri)
match (assoc match :query-params q)
;; Return uncoerced values if coercion is not enabled - so ;; Return uncoerced values if coercion is not enabled - so
;; that tha parameters are always accessible from same property. ;; that tha parameters are always accessible from same property.
parameters (or (coercion/coerce! (assoc match :query-params q)) parameters (or (coercion/coerce! match)
{:path (:path-params match) {:path (:path-params match)
:query q})] :query q})]
(assoc match :parameters parameters))))) (assoc match :parameters parameters)))))

View file

@ -1,7 +1,6 @@
(ns reitit.frontend.history (ns reitit.frontend.history
"" ""
(:require [reitit.core :as reitit] (:require [reitit.core :as reitit]
[goog.events :as e]
[reitit.core :as r] [reitit.core :as r]
[reitit.frontend :as rf] [reitit.frontend :as rf]
[reitit.impl :as impl] [reitit.impl :as impl]
@ -89,14 +88,15 @@
(-on-navigate this (-get-path this)) (-on-navigate this (-get-path this))
(assoc this (assoc this
:listen-key (gevents/listen js/window goog.events.EventType.POPSTATE handler false) :listen-key (gevents/listen js/window goog.events.EventType.POPSTATE handler false)
:click-listen-key (e/listen js/document e/EventType.CLICK ignore-anchor-click)))) :click-listen-key (gevents/listen js/document goog.events.EventType.CLICK ignore-anchor-click))))
(-on-navigate [this path] (-on-navigate [this path]
(on-navigate (rf/match-by-path router path) this)) (on-navigate (rf/match-by-path router path) this))
(-stop [this] (-stop [this]
(gevents/unlistenByKey listen-key) (gevents/unlistenByKey listen-key)
(gevents/unlistenByKey click-listen-key)) (gevents/unlistenByKey click-listen-key))
(-get-path [this] (-get-path [this]
(.. js/window -location -pathname)) (str (.. js/window -location -pathname)
(.. js/window -location -search)))
(-href [this path] (-href [this path]
path)) path))

View file

@ -20,6 +20,7 @@
{:template "/" {:template "/"
:data {:name ::frontpage} :data {:name ::frontpage}
:path-params {} :path-params {}
:query-params {}
:path "/" :path "/"
:parameters {:query {} :parameters {:query {}
:path {}}}) :path {}}})
@ -32,6 +33,7 @@
{:template "/foo" {:template "/foo"
:data {:name ::foo} :data {:name ::foo}
:path-params {} :path-params {}
:query-params {}
:path "/foo" :path "/foo"
:parameters {:query {} :parameters {:query {}
:path {}}}) :path {}}})
@ -59,6 +61,7 @@
(is (= (r/map->Match (is (= (r/map->Match
{:template "/:id" {:template "/:id"
:path-params {:id "5"} :path-params {:id "5"}
:query-params {}
:path "/5" :path "/5"
:parameters {:query {} :parameters {:query {}
:path {:id 5}}}) :path {:id 5}}})
@ -71,7 +74,7 @@
(is (= (r/map->Match (is (= (r/map->Match
{:template "/:id" {:template "/:id"
:path-params {:id "5"} :path-params {:id "5"}
;; Note: query not included in path :query-params {:mode "foo"}
:path "/5" :path "/5"
:parameters {:path {:id 5} :parameters {:path {:id 5}
:query {:mode :foo}}}) :query {:mode :foo}}})
@ -84,6 +87,7 @@
(is (= (r/map->Match (is (= (r/map->Match
{:template "/:id" {:template "/:id"
:path-params {:id "5"} :path-params {:id "5"}
:query-params {:mode "foo"}
:path "/5" :path "/5"
:parameters {:path {:id 5} :parameters {:path {:id 5}
:query {:mode :foo}}}) :query {:mode :foo}}})