Fork me on GitHub
#re-frame
<
2018-03-21
>
witek09:03:32

Is there a reason why a subscription gets only one source (the db or an other subscription)? I have started implementing my first app. I have a subscription :navig-params which provides me URL parameters (which are stored by an event handler in the db). Then I have another subscription :items which provides all "items". Now I wanted to create a third subscription :current-item which would join data from :navig-params and :items to find the item which is selected by the url. But this seams not possible in re-frame. How is this join-Problem supposed to be solved in re-frame? Thank you!

gklijs09:03:10

I have some subscriptions that depend very much on the selection in the navbar I solved it like this

(re-frame/reg-sub
  ::middle
  (fn [db]
    (case (:selected-nav db)
      :home [(:selected-nav db) (:transactions db)]
      :bank-employee [(:selected-nav db) [(:employee-iban db) (:transactions db)]]
      :client [(:selected-nav db) [(:login-status db) (:transfer-data db) (:transactions db)]]
      :background [(:selected-nav db) nil]
      [:non "error, should not be possible"])))

gklijs09:03:09

I’m not sure if thats a good way, but I like it better than having multiple layers of subscriptions and/or subsribe to the whole state

gklijs09:03:53

I try to get everyting a component needs on the same level in the main db atom, but that is not always possible

mikethompson09:03:28

@witek subscriptions can have as many inputs as needed

mikethompson09:03:37

There's no limit to 1

mikethompson09:03:59

(reg-sub 
    :something  
    
    ;; signal fn 
   (fn [_ _] 
        [(subscribe [:blah])  (subscribe [:other])])    ;; signal returns to inputs

    ;; computation fn 
    (fn [[blah other] ...]        ;; two inputs are destructured as first arg
        ... use blah and other))

👍 1
danielcompton09:03:31

@witek we don’t provide a way to get access to db and another subscription as an input, because 99.99% of the time, you should just make two subscriptions as inputs, rather than chaining off app-db directly

👍 1
danielcompton09:03:53

Whenever app-db changes, all of the subscriptions that depend on it re-run, so you want to avoid having too many subscriptions on the root