Fork me on GitHub
#re-frame
<
2023-05-30
>
BuddhiLW13:05:30

Heya, question: I have these maps, projeto1, projeto2, etc., stored in projetos which I can filter by id-projeto. I would like to reference them by :<-, but they need an id-projeto which I only have after-hand. Is there a way to do such a thing?

(rf/reg-sub
 :cp/projeto
 :<- [:cp/projetos]
 (fn [projetos [_ id-projeto]]
   (-> (filter #(= (:_id %) id-projeto) projetos)
       (first))))
This is the pseudo-code with the intent of what I want to do.
(rf/reg-sub
 :cp/identificacao-projeto
 :<- [:cp/projeto id-projeto]
 (fn [projeto [_ id-projeto]]
    (:identificacaoDoProjeto projeto)))

p-himik13:05:27

This is what I use:

(defn <-projeto
  "A signal function for `:cp/projetos`."
  [[_ id-projeto]]
  (rf/subscribe [:cp/projeto id-projeto]))

(rf/reg-sub :cp/identificacao-projeto
  <-projeto
  :-> :identificacaoDoProjeto)
I also have a slightly modified wrapper for reg-sub that removes the need for _ and that extra rf/subscribe in signal functions, so the signal function ends up being
(defn <-projeto [[id-projeto]]
  [:cp/projeto id-projeto])

BuddhiLW16:05:51

Neat! Do you have a gist for the wrapper? I never used :-> before. Interesting. Thanks!

p-himik16:05:30

Sorry, no - the wrapper has a lot more going on (despite it being a slightly modified version, I still have to copy a lot of code from around the places since re-frame is not that extensible), and is not worth sharing at its current stage. > I never used :-> before. Interesting. Then I definitely recommend reading the docstring of rf/reg-sub. :) At the very least, there's also :=>.

👀 2