Fork me on GitHub
#re-frame
<
2016-06-05
>
debug03:06:15

Pointers to why invoking the following handler causes the ratom to emit null?

(register-handler
  :load-data
  (fn [db _]
   (js/console.warn "Loading data..")
   (-> (js/fetch "")
       (.then #(.json %))
       (.then (fn [data] (mapv #(.-url %) data)))
       (.then #(dispatch [:process-data %])))))

caio04:06:49

you’re not returning db

debug04:06:20

The thing is, the contents in the db doesn't change

debug04:06:32

Just the null gets emitted causing a crash

caio04:06:43

yeah, but this handler returns the promise

caio04:06:55

and this sets the db to the promise

caio04:06:02

you should return the unchanged db anyway

debug04:06:23

Oh, didn't know that

caio04:06:26

(register-handler
  :load-data
  (fn [db _]
   (js/console.warn "Loading data..")
   (-> (js/fetch "")
       (.then #(.json %))
       (.then (fn [data] (mapv #(.-url %) data)))
       (.then #(dispatch [:process-data %])))
   db))

debug04:06:02

May I ask how re-frame knows which db to work upon?

debug04:06:35

Because I don't see

;; initial state of app-db
(def app-db {:greeting "Hello Clojure in iOS and Android!" :data (mapv str (range 1 100))})
getting registered anywhere

caio04:06:01

there is a dispatch in the core (if you’re using the template)

caio04:06:31

(register-handler
 :initialize-db
 validate-schema-mw
 (fn [_ _]
   app-db))

debug04:06:43

Yes, I'm using the template

debug04:06:57

So the first thing it gets is the db?

caio04:06:58

this does the magic, as it returns the app-db

debug04:06:18

Oh thank you, stuck for days because of this

caio04:06:24

if you want to reset the db to the default values, you can dispatch :initialize-db anytime

debug04:06:15

Does it specifically have some associating with the :initialize-db key?

debug04:06:28

Or the first thing returned to be exact?

debug04:06:59

Yes, the null is gone! 😄

caio04:06:19

nothing related to :initialize-db. it’s just the first event processed, and its handler always return the default db

caio04:06:49

take a look in init inside core.cljs. there should be a line like this: (dispatch-sync [:initialize-db])

debug04:06:47

Certainly, I really appreciate the response, I hope this is documented and that I missed to read the section

caio04:06:46

idk if it’s documented as it’s more of a pattern present in the template, rather than some re-frame characteristic

debug04:06:50

Yes, I noticed that line but didn't know it was required for the db to be returned

escherize04:06:05

@debug, When you dispatch with a key, that adds your dispatch-vector the event-queue, which uses the first item in the dispatch-vector (`:initialize-db`) to lookup what handler function to use.

debug04:06:09

I meant that the documentation regarding that db must be returned by the handler

caio04:06:38

oh, yeah, that’s documented 😆

escherize04:06:42

Next the handler function for your dispatch vector is used to swap the current value of the db

escherize04:06:00

Yeah there's a lot of docs about this 😄

debug04:06:08

I read that a few times and still I missed, a fun reading btw 🙂

caio23:06:10

their wiki is incredibly useful, I’ve never took the time to read it

danielcompton23:06:15

There’s gold in that wiki