Fork me on GitHub
#cljsrn
<
2019-10-30
>
Oliver George22:10:36

@vikeri a while back we talked about one of the things you liked about your cljsrn setup. I think it was reagent.core/track but I'm not 100% sure. I'm looking at ways to trigger dispatching (to load data) when app state changes. Here's what I came up with as a r/trigger based approach. Is that the sort of thing you were talking about?

(defn load-species-args [db]
  (let [region-pk (get-in db [:spot/state :region :pk])
        category-id (get-in db [:spot/state :category :id])]
    (when (and region-pk category-id)
      [region-pk category-id])))

(reg-event-fx
  :app/load-species
  (fn [_ [_ args]]
    {:sqlite/execute-sql {:query  (str " SELECT * FROM SPECIES WHERE REGION_ID=? AND CATEGORY_ID=? ORDER BY COMMON_NAME")
                          :args   args
                          :resp-v [:app/load-species.resp]}}))

(def *load-species-args
  (r/track #(load-species-args @re-frame.db/app-db)))

(def *load-species-args-dispatch
  (r/track! #(when-let [args @*load-species-args]
               (rf/dispatch [:app/load-species args]))))

Oliver George22:10:46

The value here being that the dispatch happens no matter how the associated state is updated - as my UI gets more interesting I'm not left trying to find all the possible event handlers which might need to dispatch.