Created
September 18, 2015 22:51
-
-
Save domgetter/ba4aa414bfb046d97d91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns reagenttest.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]) | |
(:import goog.History)) | |
;; ------------------------- | |
;; Views | |
(defn home-page [] | |
[:div [:h2 "Welcome to reagenttest"] | |
[:div [:a {:href "#/about"} "go to about page"]]]) | |
(defn about-page [] | |
[:div [:h2 "About reagenttest"] | |
[:div [:a {:href "#/"} "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))) | |
;; ------------------------- | |
;; Initialize app | |
(defn mount-root [] | |
(reagent/render [current-page] (.getElementById js/document "app"))) | |
(defn init! [] | |
(hook-browser-navigation!) | |
(mount-root)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment