Fork me on GitHub
#re-frame
<
2023-02-03
>
hifumi12305:02:54

What would be a good way to make pages load data from an API when a user navigates to it? I’ve briefly experimented with using Reitit’s controllers but I’ve found it difficult discarding cached data when routes exit, especially if several pages can share the same data. I’ve considered parametrizing my subscriptions so that every page uses “independent” data, but I’m afraid that’d put me in a bigger mess than what I’ve tried before

p-himik05:02:00

I've been using kee-frame for that without any issues for quite some time now.

hifumi12306:02:18

So using reitit controllers is fine? Do you create special events for the page or just emit events that make API calls? Originally I tried making controllers that’d dispatch api events (which on success populate the app-db). But I’m not sure of a good way to “clean up” the app-db once the user exits the route. For an example of the kind of state changes I am dealing with, say a user edits a form in /forms/:id/details. This route makes an API call to fetch some details of the form with specified id. But the /forms route itself makes an API call to fetch some data on all the forms a user has. What I’ve found tricky is being able to invalidate the cached API response in /forms so that the user can edit things in /form/:id/details and see their changes when browsing back to /forms

p-himik07:02:14

I don't know about reitit, I don't use it. I create an event per intent. > clean up the app-db kee-frame has controllers that have a :stop method - that's exactly what I'm using for any cleanup that I might need.

hifumi12307:02:12

I see. I took a quick look at kee-frame’s controllers and they seem close enough in functionality to what reitit’s controllers offer. For now I’m experimenting with dispatching a ::page/set-up and ::page/tear-down event with reitit’s controllers. The tricks I saw on https://ingesolvoll.github.io/posts/2018-06-18-kee-frame-controller-tricks/ also apply to reitit controllers. I don’t know how easy it’d be to retrofit kee-frame into an existing re-frame app that’s already invested heavily in reitit and driving most of the app state with URLs… maybe it’s something I can do in the future though. 😄