@mihaelkonjevic Is there a reason why the params-fn in a pipeline-controller/constructor, for example, must return nil
to turn off? I expected it to also turn off for false
. Did you run into some problems?
Not counting false
as a false-y value is different behavior from other predicates in clojure, and it forces code like (when (predicate? route-params) true)
rather than just (predicate? route-params)
@sooheon my reasoning was that false is still a value, while nil is absence of one. It’s a pretty arbitrary decision
are you envisioning cases where you encode something like ?paginate=false
in the route?
Not really, but I also don’t m ow what lies in the future :) . Nil vs value seemed like a good distinction because nil symbolizes absence of a controller and any value a presence of one
got it :)
really loving keechma btw, I’m learning a lot.
thanks for putting this out there
Thank you. If you need any help please reach out. There’s a lot of things in there that I wasn’t able to document properly yet, so I try to answer any questions as they come up
@mihaelkonjevic in the dataloader map, :params
takes three args: prev, route, and deps. what are prev and deps?
does prev
return the existing value (if any) at :target
?
Prev returns result of the previous loading. It will have meta and data keys. This allows you to use the previous version to check if you need to do a reload. It’s also useful if you’re doing something like infinite loading where you just append to the previously loaded data
Deps returns values of data sources that the current data source depends on
Cool, so if I don’t explicitly take prev into account, and I keep reloading the page on the same route, it will keep clearing and re-fetching the data?
Data will be refetched only if the params db returns value that is different from the previous one
It works like controller’s params fn but nil doesn’t have a special meaning. Even if you return nil, loader fn will be called
For deps example check here https://github.com/gothinkster/clojurescript-keechma-realworld-example-app/blob/master/src/cljs/realworld/datasources.cljs#L55
This data source depends on jwt data source so it can sign the requests
Deps allow you to define a graph of data sources with dependencies, and dataloader will correctly invalidate and reload datasources whose deps changed too
so the reason for jwt-datasource having that :prev
check -> ignore and no :params
is because whether to load or not doesn’t depend on route, but on whether the jwt exists in local-storage or not?
Yes and once it exists you don’t need to load it again. If the user logs out, you can remove jwt manually from a controller and then force a dataloader reload.
Got it. Also, if I tried to mock :loader
to return just data like:
(map-loader
(fn [req]
(when (:params req)
;; TODO
;; (ajax/GET "/api/stats/total")
(print "loading data...")
[{:id 1 :clicks 100 :impressions 200}
{:id 2 :clicks 300 :impressions 700}])))
but this doesn’t work. How do you test them?Hm, this looks like it should work
Where else do I need to hook up dataloaders other than app-definition?
Do you have a dataloader controller running?
No, I guess that’s it.
I thought just defining the datasource was enough
is this it?
Thanks, it’s working :)