secretary icon indicating copy to clipboard operation
secretary copied to clipboard

Question: clicking on links does not trigger dispatch?

Open mac01021 opened this issue 9 years ago • 1 comments

This is probably PEBCAK.
I have the following tiny application using secretary and reagent. Calls to secretary/dispatch! like the one at the bottom of the file update the application as expected. Clicking on the links, however, does not result in a call to dispatch. Any pointers you can provide will be appreciated. Thanks in advance!

(ns askit-client.main
    (:require [reagent.core :as reagent]
              [secretary.core :as secretary :refer-macros [defroute]]))

(enable-console-print!)


;; Define reagent components
(defn home []
    [:div [:h1 "Welcome, friend!"]
        [:div [:a {:href "#/about"} "about us"]]])
(defn about []
    [:div "Now you know all about us!"
        [:div [:a {:href "#/"} "go home"]]])
(defn current-page [] [(@app-state :current-page)])




;; Set up application state
(def app-state (reagent/atom {}))

(defn put! [k v] (swap! app-state assoc k v))

(put! :current-page home)



;; Define secretary routes
(secretary/set-config! :prefix "#")
(defroute "/" []
    (println "time to go home")
    (put! :current-page home))
(defroute "/about" []
    (println "time to go about")
    (put! :current-page about))



(println "Let's mount this thing and go home!")
(reagent/render-component [current-page] (.getElementById js/document "app"))

(secretary/dispatch! "/about")

mac01021 avatar Jul 17 '15 17:07 mac01021

currently secretary doesn't add any click handlers to the page, but you can do something along the lines of:

(events/listen js/document "click"
               (fn [e]
                 (let [path (.getPath (.parse Uri (.-href (.-target e))))
                       title (.-title (.-target e))]
                   (when (secretary/locate-route path)
                     (. e preventDefault)
                     (. history (setToken path title))))))

gf3 avatar Aug 10 '15 20:08 gf3