Anyone would have an example of a global interceptor that throws event whenever a given value in the DB changes?
you could do this via a subscription and exploit that a subscription is only recomputed when an upstream value changes
It's better to use a global interceptor for that instead of exploiting something, because that's exactly why global interceptors have been introduced.
Check out re-frame.core/on-changes
.
oh, I missed those
is there a tool for creating static(html) documentations for events and subscriptions?
@genekim worked on this a few months ago, he may have a few links
really cool! thanks to both of you 🙏
Yes, I just miss a single example of global interceptor
Thanks a lot for your help p-himik
A global interceptor is just a regular interceptor registered with re-frame.core/reg-global-interceptor
. There are no other differences whatsoever.
I have a question regarding re-frame and routing with reitit.
Given the following routes:
(def router
(reitit/router
[["/" {:name :home
:view #'home-page
:controllers [{:start (fn [_] (rf/dispatch [:page/init-home]))}]}]
["/about" {:name :about
:view #'about-page}]
["/login" {:name :login
:view #'login-page}]]))
And the following re-frame definitions (mostly taken from the Luminus template):
;;dispatchers
(rf/reg-event-db
:common/navigate
(fn [db [_ match]]
(let [old-match (:common/route db)
new-match (assoc match :controllers
(rfc/apply-controllers (:controllers old-match) match))]
(assoc db :common/route new-match))))
(rf/reg-fx
:common/navigate-fx!
(fn [[k & [params query]]]
(rfe/push-state k params query)))
(rf/reg-event-fx
:common/navigate!
(fn [_ [_ url-key params query]]
{:common/navigate-fx! [url-key params query]}))
(rf/reg-event-fx
:login
(fn [_ _]
{:http-xhrio {:method :get
:uri "/api/login"
:headers {:Authorization "Basic xxxxxxx=="}
:response-format (ajax/raw-response-format)
:on-success [:common/navigate ??????]}}))
;;subscriptions
(rf/reg-sub
:common/route
(fn [db _]
(-> db :common/route)))
(rf/reg-sub
:common/page-id
:<- [:common/route]
(fn [route _]
(-> route :data :name)))
(rf/reg-sub
:common/page
:<- [:common/route]
(fn [route _]
(-> route :data :view)))
Assuming a successful login, what code do I associate with the :on-success
in the :login
event handler, so that the application switch to another route, e.g. /about
and its associated page?
I suspect I should dispatch a :common/navigate
event but what would be its argument then?
My :common/navigate
is dispatched from a function on-navigate
passed to reitit.frontend.easy/start!
, which I assume is triggered from reitit.frontend.easy/push-state!
from an effect dispatched by an event handler I pass the route name and any params. A bit of a mouthful, but that's how it's done in https://github.com/metosin/reitit/blob/master/examples/frontend-re-frame/src/cljs/frontend_re_frame/core.cljs
@regen Thank you very much. I got it working.
Glad to hear it helped!
Hm, how do I debug errors with http-fx? Seems like whatever I do, I run into the last-error “[0]“, :last-error-code 6 - type response…