Fork me on GitHub
#re-frame
<
2017-09-13
>
joshkh09:09:36

how do i go about using values passed to a dynamic subscription? in this example i need the 'specific-id' value in my second function:

(reg-sub
  ::one-list
  (fn [[_ specific-id]]
    [(subscribe [:lists/filtered-lists])])
  (fn [[filterd-lists]]
    ; Need access to specific-id for something like:
    ; (filter #(= specific-id (:id %)) filterd-lists)
    ))

joshkh09:09:18

this doesn't work because the second function expects only input signals:

(reg-sub
  ::one-list
  (fn [[_ specific-id]]
    [specific-id (subscribe [:lists/filtered-lists])])
  (fn [[specific-id filterd-lists]]
    ; Need access to specific-id for something like:
    ; (filter #(= specific-id (:id %)) filterd-lists)
    ))

lsnape09:09:59

@joshkh bit of a hack, but could you wrap specific-id in a reaction ? Not tried that so not sure if it would work, or if it’s advised!

joshkh10:09:52

oh, clever!

kasuko14:09:24

@joshkh For the computation function in a subscription it takes 2 arguments. The first is the input signals and the second is the subscription vector. Thus you should be able to do

kasuko14:09:19

(reg-sub
  ::one-list
  (fn [[_ specific-id]]
    (subscribe [:lists/filtered-lists]))
  (fn [filtered-lists [_ specific-id]]
    ; Need access to specific-id for something like:
    (filter #(= specific-id (:id %)) filtered-lists)
    ))

kasuko14:09:57

Infact since you aren’t even using the specific-id in the inputs function you can use the syntatic sugar

kasuko14:09:20

(reg-sub
  ::one-list
  :<- [:lists/filtered-lists]
  (fn [filtered-lists [_ specific-id]]
    ; Need access to specific-id for something like:
    (filter #(= specific-id (:id %)) filtered-lists)
    ))

joshkh14:09:01

that's weird, i could have sworn i wasn't getting the argument vector in your first suggestion:

(reg-sub
  ::one-list
  (fn [[_ specific-id]]
    (subscribe [:lists/filtered-lists]))
  (fn [filtered-lists [_ specific-id]]
    ; Need access to specific-id for something like:
    (filter #(= specific-id (:id %)) filtered-lists)
    ))
i'll go back and take another look. thanks, @kasuko

kasuko14:09:47

Is this called via (re-frame/subscribe [::one-list "1234"])?

joshkh14:09:03

i might have just been destructuring incorrectly 🙂

kasuko14:09:19

Cool, glad it works!

sandbags15:09:59

I am honestly puzzled that there are people who use reagent without using re-frame