Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: clicking on links does not trigger dispatch? #70

Open
mac01021 opened this issue Jul 17, 2015 · 1 comment
Open

Question: clicking on links does not trigger dispatch? #70

mac01021 opened this issue Jul 17, 2015 · 1 comment
Labels

Comments

@mac01021
Copy link

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")
@gf3
Copy link
Collaborator

gf3 commented Aug 10, 2015

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 gf3 added the question label Aug 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants