Fork me on GitHub

I'm trying to simplify the re-frame reg-event-db function, and I have the following macro so far:

(defmacro db-event [event-key params & body]
     (re-frame.core/reg-event-db ~event-key
                                 (fn [~'db [_# [email protected]]]
                                   [email protected]))
     (defn ~(symbol event-key) ~params (re-frame.core/dispatch [~event-key [email protected]]))))
And it works pretty well, in that I can use it like so:
(db-event :remove-from-cart [product-id]
          (assoc-in db [:cart product-id] (-> db :cart (get product-id) dec)))
However, I want to further simply it, and there shouldn't be a need to write assoc-in. So I would like a functionality like this:
(db-event :foo [resp] {:abc {:def resp :efg (more-stuff resp)} :foo1 {:bar1 (stuff resp)}})
instead of :
(db-event :foo [resp]
           (assoc-in db [:abc :def] resp)
           (assoc-in db [:abc :efg] (more-stuff resp))
           (assoc-in [:foo1 :bar1] (stuff resp))))
So basically, I want to give a map which tells which values are to be changed rather than writing a bunch of assoc-ins in succession. How can I do that?

👍 4

db is a map, so the {:abc {:def resp ..} why not just merge them then?


Perhaps look up the path interceptor


what's that and why would that be needed?


It is used in the todomvc sample


how does it work?


Best if you read the links


but can you give an example how path interceptor is used?


That's EXACTLY what the links do


yeah okay, that's not the point is it? The example shows a single path (path :todos), but what I need is to give a map which contains all the keys and the values that are to be changed. Really more to do with macros than with re-frame.