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

preventing navigation on clicking anchor tag with href #85

Open
jdkealy opened this issue May 27, 2016 · 1 comment
Open

preventing navigation on clicking anchor tag with href #85

jdkealy opened this issue May 27, 2016 · 1 comment

Comments

@jdkealy
Copy link

jdkealy commented May 27, 2016

I just put together a minimal example recreating something I have been experienced. I was wondering if it were possible for clicking on an href not to retrigger the router. It seems that something in the secretary library is listening to the click event before my click handler has the chance to prevent it from doing so. Is it possible to keep the route's handler from being fired?

(ns minimal-example.core
    (:require [reagent.core :as reagent :refer [atom]]
              [reagent.session :as session]
              [secretary.core :as secretary :include-macros true]
              [accountant.core :as accountant]))

;; -------------------------
;; Views

(defn first-tab []
  [:div "FIRST TAB!"])

(defn second-tab []
  [:div "SECOND TAB!"])

(def selected-tab (atom nil))

(def tabs [{:key "one"
            :name "one"
            :cmp first-tab}
           {:key "two"
            :name "two"
            :cmp second-tab}])

(defn home-page []
  (reagent/create-class
   {:component-did-mount (fn []
                           (reset! selected-tab (-> tabs
                                                    first
                                                    :key)))
    :reagent-render (fn []
                      [:div
                       [:ul
                        (doall
                         (map (fn [item]
                                [:li
                                 [:a {:href "#"
                                      :on-click (fn [e]
                                                (reset! selected-tab (:key item))
                                                (.preventDefault e)
                                                (.preventPropogation e)
                                                false)}
                                  (:name item)]]
                                ) tabs))]
                       (doall
                        (map (fn [{:keys [key cmp]}]
                              (when (= key @selected-tab)
                                [cmp]))
                             tabs))])}))

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

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

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

(defn init! []
  (accountant/configure-navigation!
    {:nav-handler
     (fn [path]
       (secretary/dispatch! path))
     :path-exists?
     (fn [path]
       (secretary/locate-route path))})
  (accountant/dispatch-current!)
  (mount-root))
@njj
Copy link

njj commented May 22, 2019

@jdkealy I know this is super old, but did you ever figure this out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants