0

I am currently learning reagent with secretary as its route. I find that I can use query-params to get a hash-map of all parameters with question mark (?) like ?name=Daniel

(ns feampersanda.core
    (:require-macros [secretary.core :refer [defroute]])
    (:import goog.History)
    (:require
      [secretary.core :as secretary]
      [goog.events :as events]
      [goog.history.EventType :as EventType]
      [reagent.core :as r]))

;; ------------------------------
;; States
;; page --> is occupied by page state
(def app-state (r/atom {:params {}}))

;; ------------------------------
;; History

(defn hook-browser-navigation! []
  (doto (History.)
    (events/listen
     EventType/NAVIGATE
     (fn [event]
       (secretary/dispatch! (.-token event))))
    (.setEnabled true)))

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


;; -------------------------
;; Parameters

(defn update-query-params! [query-params]
  (do
    (js/console.log (str query-params))
    (swap! app-state assoc-in [:params :query] query-params))
  )

;; -------------------------
;; Routing Config

(defn app-routes []
  (secretary/set-config! :prefix "#")

  (defroute "/" [query-params]
            (do
              (update-query-params! query-params)
              (swap! app-state assoc :page :home)))

  (defroute "/about/:id" [id query-params]
            (do
              (js/console.log id)
              (update-query-params! query-params)
              (swap! app-state assoc :page :about)))


  (hook-browser-navigation!))

(defmulti current-page #(@app-state :page))

(defmethod current-page :home []
  [:div [:h1 (str "Home Page")]
   [:a {:href "#/about"} "about page"]
   [:br]
   [:a {:href "#/about"} (str (:count @app-state))]
   ])

(defmethod current-page :about []
  [:div [:h1 "About Page"]
   [:a {:href "#/"} (str "home page" " --> "
                         (:yes (:query (:params @app-state)))
                         )]])

(defmethod current-page :default []
  [:div
   [:p "404"]
   ])


;; -------------------------
;; Initialize app

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

(defn init! []
  (mount-root))

I don't know how to pass the id parameter to a defmethod, so I want it to be saved inside an atom, so I wonder how to get hash-map which is include all of the named parameters like http://0.0.0.0:3449/#/about/black/12 to {:path "black" :id "12"}

Ampersanda
  • 2,016
  • 3
  • 21
  • 36

1 Answers1

0

One solution would be to use cemerick's URL library

(require '[cemerick.url :as url])
(keys (:query (url/url (-> js/window .-location .-href))))

https://github.com/cemerick/url

Hlöðver
  • 101
  • 1
  • 7
  • Thank you for your reply. That's a new knowledge about parse the parameters but the code that you use above is getting the `:query` hash-map which is I can accomplish with only using `query-params` parameter in `secretary`. I add some words in my question which is I want to get the `named parameters` like `http://localhost:3449/about/:id/:path` to `{:id "blah" :path "blah"}`. Sorry if my words are not to good to explain – Ampersanda Nov 28 '17 at 13:41
  • Can you include in your URL example where you want to "blah" to be found. Without hashtag, the slashes will be looked for in your backend handler (compojure for example). I don't know better way of handling key-value pairs in URL's than with query parameters. – Hlöðver Nov 28 '17 at 13:56