Fork me on GitHub
#re-frame
<
2019-01-14
>
victorb13:01:50

I'm sure I've read about this in the re-frame docs or some other place, but can't find it. Trying to figure out how to do "nested" subscriptions (one subscription is for :setup in db, next one for -> :setup :password for example)

manutter5113:01:52

Sure, I do that all the time. You can use the output from one subscription as the input to another. See the comments in this file for detailed docs: https://github.com/Day8/re-frame/blob/master/examples/todomvc/src/todomvc/subs.cljs

victorb13:01:33

hah, fun, I just came across this and came up with this, based on that very file:

(reg-sub
  :setup
  (fn-traced [db _]
             (:setup db)))

(reg-sub
  :setup-password
  (fn-traced [_ _]
             (:password @(subscribe [:setup]))))

victorb13:01:49

Thanks a lot @manutter51 🙂

victorb13:01:50

hm, one difference, I put @(subscribe) (with @) but the subs.cljs doesn't have that. Don't you need to deref it to get the actual value?

manutter5113:01:24

I wouldn’t recommend putting a subscribe inside a subscription handler, there’s a better way.

manutter5113:01:58

(reg-sub
 :setup
 (fn-traced [db _]
   (:setup db)))

(reg-sub
 :password 
 :<- [:setup]
 (fn [setup]
   (:password setup)))

Lu20:01:21

Was just wondering if one could use this pattern when the subscription setup requires an argument ...

manutter5120:01:34

It should, but you might not be able to use the syntactic sugar. The “manual” way is documented in the comments about Layer 3 subs here: https://github.com/Day8/re-frame/blob/master/examples/todomvc/src/todomvc/subs.cljs#L31

Lu19:01:25

Oh great!!

victorb13:01:13

Yeah, makes sense. Thanks again 🙂

manutter5113:01:20

Heh, yeah, you got to it while I was trying to type out the example 🙂

victorb13:01:44

indeed. None the less, thanks for the pointers!

victorb13:01:54

is there a similar pattern for reg-event-db? (basically nesting them)

manutter5113:01:32

You can use reg-event-fx and return a map with keys for :dispatch (a single new event to dispatch) or :dispatch-n (a vector of events to dispatch), or :dispatch-later (a tuple of event-to-dispatch + time-to-delay, I forget which order they’re in)

manutter5113:01:21

:reg-event-db is a simplification of :reg-event-fx, for the most common use-case.

victorb14:01:17

@manutter51 I see. Might be able to work something out with that. Thanks

manutter5114:01:22

Be sure to read the docs for reg-event-fx — it takes slightly different args and returns a different value than :reg-event-db

đź‘Ť 5
Jacob Haag16:01:38

Hey everybody, since re-frame is mostly in .cljc files I have been trying to exercise it with the Clojure REPL. This has lead to problems so I've opened an issue on re-frame's github. If any of you have any insight please feel free to comment on it. https://github.com/Day8/re-frame/issues/522

WhoNeedszZz22:01:31

The answer is on the page that you referenced in your issue: https://github.com/Day8/re-frame-test

Jacob Haag13:01:26

@UAFT1B7Q8 can you be more specific?

WhoNeedszZz13:01:06

Did you read the link I provided?

Jacob Haag14:01:10

I've read it a few times. I get the same arity issue using the framework when I run tests with clojure.test

WhoNeedszZz14:01:32

You didn't dereference the subscriptions.

Jacob Haag14:01:40

Still the same error

(let [typeahead @(re-frame/subscribe [::typeahead]                                         )]
                                    #_=>   typeahead)

Execution error (ArityException) at re-frame.subs/reg-sub$subs-handler-fn (subs.cljc:325).
Wrong number of args (1) passed to: slm.components.typeahead.subscriptions/eval27322/fn--27323