Skip to content

Instantly share code, notes, and snippets.

@facundoolano
Created December 4, 2017 14:51
Show Gist options
  • Save facundoolano/7bbf2de25479b3c58ee21c67d2975bd5 to your computer and use it in GitHub Desktop.
Save facundoolano/7bbf2de25479b3c58ee21c67d2975bd5 to your computer and use it in GitHub Desktop.
navigation2.clj
(re-frame/reg-event-fx
:navigate
(fn [{:keys [db]} [_ new-view]]
{;; trigger the load-view event in case data from the server is required
:dispatch [:load-view new-view]
;; update the browser history API and switch to the given view
:set-history new-view
;; set the current view in the app-db so the dom is updated
:db (assoc db
:loading-view? true
:current-view new-view)}))
(defmulti load-view
"Create a multimethod that will implement different event handlers based on the
view keyword."
(fn [cofx [view]] view))
(re-frame/reg-event-fx
:load-view
(fn [cofx [_ new-view]]
;; delegate event handling to the proper multimethod
(load-view cofx [vector new-view])))
(defmethod load-view
:default
[{:keys [db]} _]
;; by default don't load anything
{:db (assoc db :loading-view? false)})
(defmethod load-view
:channel-list
[_ _]
;; when navigating to the channel list, fetch the channels
{:http-xhrio {:method :get
:uri "/api/channels"
:on-success [:channel-list-success]}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment