1

I have application routes defined in my routes.cljs file in the root of src/cljs/project/routes.cljs.

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

  (defroute
    "/" []
    (re-frame/dispatch [::events/set-active-panel :welcome-panel])))
  ; shortened for brevity

It is initialized in core.cljs

; required [portfolio-app.events :as events]
(defn ^:export init []
  (routes/app-routes)
  (re-frame/dispatch-sync [::events/initialize-db])
  (dev-setup)
  (mount-root))

It is dispatched to the ::events/set-active-panel in events.cljs

(re-frame/reg-event-db
 ::set-active-panel
 (fn-traced [db [_ active-panel]]
   (assoc db :active-panel active-panel)))

And has the :active-panel subscription in subs.cljs

(re-frame/reg-sub
 ::active-panel
 (fn [db _]
   (:active-panel db)))

I subscribe to :active-panel in my layout.cljs

; required [portfolio-app.subs :as subs]
(defn panel []
  (let [active-panel (re-frame/subscribe [::subs/active-panel])]
    [:div
     "which panel? " @active-panel]))

@active-panel is nil when I access a page for the first time. The panel is dispatched only when I navigate through the pages. I know this worked initially. I can't see anything in my commits that could have broken it.

How do I get my defroutes to fire on page load as well as through site navigation?

Clarice Bouwer
  • 3,631
  • 3
  • 32
  • 55

2 Answers2

0

A couple guesses based on the code available:

  • Are you calling secretary/dispatch on page load or just on subsequent navigation?
  • Is your ::events/initialize-db initializing the db after the :active-panel is properly set, causing your subscription to return nil?
bostonou
  • 1,194
  • 10
  • 21
0

I suspect you have fallen victim to the re-frame "gotcha", where a namespace like events is elided in the compilation process since it is not listed in a (:require ...) form. See the Gotcha documentation for more details.

To make the (:require ...) more explicit and harder to accidentally forget, I always wrap all of the (reg-event-* ...) calls inside a larger function that is initialized from the main program:

(ns demo.core  
  (:require 
     [demo.events :as events]
     [demo.subs :as subs]
     ...))

(defn app-start
  "Initiates the cljs application"
  []
  (events/define-all-events!)
  (subs/initialize-all)
  (configure-browser-routing!)
  ...)

and then:

(ns demo.events ...)
(defn define-all-events! []
  (reg-event-db ...)
  (reg-event-fx ...)
  ...)

I use a similar "wrap it in a function" technique for secretary/accountant routing and also for defining subscriptions (i.e. reg-sub). For example:

(defn configure-browser-routing! []
  (println "configure-browser-routing - enter")

  (secretary/defroute "/all" []
    (println :secretary-route-all)
    (browser-nav-handler :all))
  (secretary/defroute "/active" []
    (println :secretary-route-active)
    (browser-nav-handler :active))
  (secretary/defroute "/completed" []
    (println :secretary-route-completed)
    (browser-nav-handler :completed))
  (secretary/defroute "/*" []
    (println ":secretary-route-all  *** default ***")
    (browser-nav-handler :all))

  (accountant/configure-navigation!
    {:nav-handler  (fn [path]
                     (t/spy :accountant--nav-handler--path path)
                     (secretary/dispatch! path))
     :path-exists? (fn [path]
                     (t/spy :accountant--path-exists?--path path)
                     (t/spy :accountant--path-exists?--result
                       (secretary/locate-route path)))})
  (println "configure-browser-routing - leave"))
Alan Thompson
  • 29,276
  • 6
  • 41
  • 48
  • Thanks for your help. Do you by any chance have a repo I can look at so that I can see where I am going wrong? `init` won't mount when I wrap my functions and I suspect I must have one or two things misplaced or incorrectly wrapped. Much appreciated. – Clarice Bouwer Apr 30 '19 at 07:02
  • You can clone the following repo. Note that you need the `secretary-accountant` branch. https://github.com/cloojure/cljs-enflame/blob/secretary-accountant/src/cljs/flintstones/core.cljs – Alan Thompson Apr 30 '19 at 07:31