Skip to content

Instantly share code, notes, and snippets.



Created Jun 6, 2020
What would you like to do?
Fragment issue
(ns frontend-re-frame.core
(:require [re-frame.core :as re-frame]
[reagent.core :as reagent]
[reitit.core :as r]
[reitit.coercion.spec :as rss]
[reitit.frontend :as rf]
[reitit.frontend.controllers :as rfc]
[reitit.frontend.easy :as rfe]))
;;; Events ;;;
(fn [_ _]
{:current-route nil}))
(fn [db [_ & route]]
;; See `navigate` effect in routes.cljs
{::navigate! route}))
(fn [db [_ new-match]]
(let [old-match (:current-route db)
controllers (rfc/apply-controllers (:controllers old-match) new-match)]
(assoc db :current-route (assoc new-match :controllers controllers)))))
;;; Subscriptions ;;;
(fn [db]
(:current-route db)))
;;; Views ;;;
(defn home-page []
(fn []
[:h1 "This is home page"]
;; Dispatch navigate event that triggers a (side)effect.
{:on-click #(re-frame/dispatch [::navigate ::sub-page1])}
"Go to sub-page 1"]
[:a {:href "/sub-page1#foo"} "Go to sub-page 1 and fragment foo."]]))
(defn sub-page1 []
[:h1 {:style {:height "2000px"}} "This is sub-page 1"]
[:p {:id "foo"}
"Fragment identified paragraph"]])
;;; Effects ;;;
;; Triggering navigation from events.
(fn [route]
(apply rfe/push-state route)))
;;; Routes ;;;
(defn href
"Return relative url for given route. Url can be used in HTML links."
(href k nil nil))
([k params]
(href k params nil))
([k params query]
(rfe/href k params query)))
(def routes
{:name ::home
:view home-page
:link-text "Home"
[{;; Do whatever initialization needed for home page
;; I.e (re-frame/dispatch [::events/load-something-with-ajax])
:start (fn [& params](js/console.log "Entering home page"))
;; Teardown can be done here.
:stop (fn [& params] (js/console.log "Leaving home page"))}]}]
{:name ::sub-page1
:view sub-page1
:link-text "Sub page 1"
[{:start (fn [& params] (js/console.log "Entering sub-page 1"))
:stop (fn [& params] (js/console.log "Leaving sub-page 1"))}]}]])
(defn on-navigate [new-match]
(when new-match
(re-frame/dispatch [::navigated new-match])))
(def router
{:data {:coercion rss/coercion}}))
(defn init-routes! []
(js/console.log "initializing routes")
{:use-fragment false}))
(defn nav [{:keys [router current-route]}]
(for [route-name (r/route-names router)
:let [route (r/match-by-name router route-name)
text (-> route :data :link-text)]]
[:li {:key route-name}
(when (= route-name (-> current-route :data :name))
"> ")
;; Create a normal links that user can click
(js/console.log route-name)
[:a {:href (if (= route-name ::sub-page1)
(href route-name)
;; (href route-name)
)} text]])])
(defn router-component [{:keys [router]}]
(let [current-route @(re-frame/subscribe [::current-route])]
[nav {:router router :current-route current-route}]
(when current-route
[(-> current-route :data :view)])
;;; Setup ;;;
(def debug? ^boolean goog.DEBUG)
(defn dev-setup []
(when debug?
(println "dev mode")))
(defn mount-root []
(init-routes!) ;; Reset routes on figwheel reload
(reagent/render [router-component {:router router}]
(.getElementById js/document "app")))
(defn ^:export init []
(re-frame/dispatch-sync [::initialize-db])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.