clj-commons / secretary

A client-side router for ClojureScript.
773 stars 64 forks source link

Add HTML5 history Example #74

Open dqdinh opened 9 years ago

dqdinh commented 9 years ago

Would be nice to have a reference example that does not use the # prefix.

andromorv commented 9 years ago
(ns frontend.routes
  (:require [secretary.core :as sec :refer-macros [defroute]]
            [goog.events :as events]
            [goog.history.EventType :as EventType]
            [frontend.state :refer [change-state]])
  (:import goog.history.Html5History))

(defonce history (Html5History.))

;; We can switch routes through this function
(defn trans
  [path]
  (.setToken history path))

(defroute "/" []
          (change-state ...))

(defroute "/post/:slug" {:as params}
          (change-state ...))

(defn- on-popstate
  [e]
  (-> e .-token sec/dispatch!))

(events/listen history EventType/NAVIGATE on-popstate)

;; We can remove # prefix this way
(doto history (.setEnabled true)
              (.setPathPrefix "")
              (.setUseFragment false))
BorisKourt commented 9 years ago

There is now Accountant which handles this and a bit more as well? Not that that closes this issue.