Fork me on GitHub
#re-frame
<
2019-06-20
>
credulous18:06:18

Hi! I’m wondering what the best way is to make a level-3 sub that needs to access the db in the second function.

credulous18:06:04

I have a ui element that changes a list of selected filters (companies, in this case). The sub for that is

(rf/reg-sub
  :companies
  (fn [db _]
    (:companies db)))

credulous18:06:58

There is another vector in the app-db, under :facilities. Each map in that vector includes a company ID.

credulous18:06:30

I’d like a subscription that filters the :facilities vector according to what’s in the :selected-companies vector.

credulous18:06:32

The trouble is if I don’t know how to get to facilities in the actual subscription function.

credulous18:06:41

(rf/reg-sub
  :selected-facilities
  (fn [_ _] (rf/subscribe [:selected-companies]))
  (fn [companies _]
    ... how to get to app-db/facilities?
    ))

Thomas Karras18:06:04

I think this should work

(re-frame/reg-sub
  :selected-facilities
  :<- [:selected-companies]
  :<- [:facilities]
  (fn [[selected-companies facilities]]
   ...))

credulous18:06:25

OK, thanks! That’s the first time I’ve seen that syntax… is there someplace I can learn more?

Thomas Karras18:06:24

the example in the re-frame docs go into more explanation of that syntax, https://github.com/Day8/re-frame/blob/master/examples/todomvc/src/todomvc/subs.cljs

credulous18:06:35

Thanks! I’ll have to read the source of the todo example more carefully, that’s very helpful.

kaneko22:06:20

Hey everyone, I'm trying to post a cljs map as json with query params using http-fx, but haven't been able to succeed. The error thrown by the API is that it expected JSON, for some reason I think I am sending an empty body. Could someone point me in the right direction please? This is my http-xhrio map:

Clojure
{:uri ""
                   :method :post
                   :format (ajax/json-request-format)
                   :headers {:authorization (utils/basic-auth username pwd)}
                   :timeout 10000
                   :url-params {:foo foo
                                :bar bar}
                   :body my-cljs-map
                   :response-format (ajax/json-response-format {:keywords? true})
                   :on-success [:success]
                   :on-failure [:failure]}

kaneko22:06:23

Turns out I needed to use :params (clj->js my-map) instead of :body my-map

valtteri04:06:08

clj->js is not needed when using :params. Here’s relevant docs from underlying cljs-ajax https://github.com/JulianBirch/cljs-ajax#getpostput

kaneko11:06:01

Thanks for the help guys 🙂