Skip to content
Permalink
Browse files

Switch to bidi and pushy for routing.

  • Loading branch information...
pupeno committed Sep 8, 2015
1 parent 5225ecd commit e147a5c935421bd1fba3fc13ad5ff6e7bb65939c
Showing with 21 additions and 26 deletions.
  1. +2 −1 project.clj
  2. +19 −25 src/cljs/projectx/core.cljs
@@ -17,7 +17,8 @@
[hiccup "1.0.5"]
[environ "1.0.0"]
[org.clojure/clojurescript "1.7.122" :scope "provided"]
[secretary "1.2.3"]]
[bidi "1.20.3"]
[kibu/pushy "0.3.2"]]

:plugins [[lein-environ "1.0.0"]
[lein-asset-minifier "0.2.2"]]
@@ -1,51 +1,45 @@
(ns projectx.core
(:require [reagent.core :as reagent :refer [atom]]
[reagent.session :as session]
[secretary.core :as secretary :include-macros true]
[goog.events :as events]
[goog.history.EventType :as EventType])
[bidi.bidi :as bidi]
[pushy.core :as pushy])
(:import goog.History))

;; -------------------------
;; Views
;; Routes
(def routes ["/" {"" :home-page
"about" :about-page}])

;; -------------------------
;; Views
(defn home-page []
[:div [:h2 "Welcome to projectx"]
[:div [:a {:href "#/about"} "go to about page"]]])
[:div [:a {:href (bidi/path-for routes :about-page)} "go to about page"]]])

(defn about-page []
[:div [:h2 "About projectx"]
[:div [:a {:href "#/"} "go to the home page"]]])
[:div [:a {:href (bidi/path-for routes :home-page)} "go to the home page"]]])

(defn current-page []
[:div [(session/get :current-page)]])

;; -------------------------
;; Routes
(secretary/set-config! :prefix "#")

(secretary/defroute "/" []
(session/put! :current-page #'home-page))

(secretary/defroute "/about" []
(session/put! :current-page #'about-page))

;; -------------------------
;; History
;; must be called after routes have been defined
(defn hook-browser-navigation! []
(doto (History.)
(events/listen
EventType/NAVIGATE
(fn [event]
(secretary/dispatch! (.-token event))))
(.setEnabled true)))
;; Routing and wiring
(defn parse-path [path]
(case (:handler (bidi/match-route routes path))
:home-page #'home-page
:about-page #'about-page
(throw (js/Error. (str "Path not recognized: " (pr-str path))))))

(defn set-current-page [parsed-path]
(session/put! :current-page parsed-path))

;; -------------------------
;; Initialize app
(defn mount-root []
(reagent/render [current-page] (.getElementById js/document "app")))

(defn init! []
(hook-browser-navigation!)
(pushy/start! (pushy/pushy set-current-page parse-path))
(mount-root))

0 comments on commit e147a5c

Please sign in to comment.
You can’t perform that action at this time.