Fork me on GitHub
#re-frame
<
2016-04-26
>
fasiha01:04:48

In db/default-db (which initializes app-db), I fire off an HTTP request via goog.net.XhrIo and invoke a re-frame handler in its callback. However, the request must complete before re-frame has a chance to set up handlers because I see re-frame: no event handler registered for: ":coast". Ignoring.. Am I misguided in my attempt to put a network request in the default db? (I was moved to do this because I set up a websocket in default-db too, and its callback also dispatches a re-frame handler, but I guess the websocket doesn't get any data before the handlers are all loaded.)

fasiha01:04:24

Ok, I initialized the value in default-db to nil, and in the view, I check to see if a subscription to that value is nil, and if so, fires off an HTTP request, etc.

fasiha01:04:35

That seems to delay the request long enough for handlers to be loaded

mikethompson02:04:40

Sounds like you are heading in the right direction. Typically: 1. your main normally does this (dispatch-sync [:startup]) 2. In the handler for :startup (1) some initial state is established in app-db which indicates the GUI should show "Loading ..." (or whatever makes sense for no data) and (b) HTTP call is made 3. When the HTTP response comes back it is handled by a further dispatch 4. In the handler for the response, you put in the real data. Which then causes the UI to render something more than "Loading ..."

mikethompson02:04:14

@fasiha: ^^^^ So, I wouldn't be putting any HTTP GETs in default-db

fasiha03:04:57

@mikethompson: ahh, the lein template did put a dispatch-sync in my app.core/init! Thank you!

fasiha03:04:59

Moved that request there 😄 now I feel warm and fuzzy instead of green and clammy

curlyfry12:04:33

It seems like the rule of thumb there is to use local atoms when making reusable components, and subscribing in the larger views

curlyfry12:04:17

Which leads me to believe that maybe alternative 1. is not a bad choice when doing things such as changing the appearance of a reusable component based on the scroll event