Fork me on GitHub
#reitit
<
2019-08-19
>
jahson20:08:29

Correct me if I'm wrong: if I need the frontend controller to fire each time I navigate to the route, even when I'm on the same route, I need to provide :identity fn that will return different value on each call. The route has no parameters, so that is out of concern.

juhoteperi13:08:41

Yes. You could implement identity fn like you describe, but it could be controllers are not good fit for this use case. You can implement some alternative solution in your route change fn: https://github.com/metosin/reitit/blob/master/examples/frontend-controllers/src/frontend/core.cljs#L80 E.g. check route-data for option which contains function, or re-frame event vector and dispatch that. Or something.

jahson10:08:23

There's one side-effect of my approach when using Html5History — each time any a with :href "#" element is clicked, the controller will be applied. Leaving aside the questionable practice of using :href "#" (a legacy code in my case), the reason is that the property -href will have value location + "#", which will cause the code in global handler ignore-anchor-click to work (https://github.com/metosin/reitit/blob/master/modules/reitit-frontend/src/reitit/frontend/history.cljs#L89-L101). You probaly want to know this, if you plan to use controller with :identity that is always different and there are anchors with :href "#" in your code.

juhoteperi10:08:40

There is open PR to make the click predicate fn configurable: https://github.com/metosin/reitit/pull/259

jahson10:08:45

Thanks, I've forgot to check the issues. At the moment I will, probably, remove the :href "#" part from the code.

juhoteperi13:08:41

Yes. You could implement identity fn like you describe, but it could be controllers are not good fit for this use case. You can implement some alternative solution in your route change fn: https://github.com/metosin/reitit/blob/master/examples/frontend-controllers/src/frontend/core.cljs#L80 E.g. check route-data for option which contains function, or re-frame event vector and dispatch that. Or something.

jahson10:08:23

There's one side-effect of my approach when using Html5History — each time any a with :href "#" element is clicked, the controller will be applied. Leaving aside the questionable practice of using :href "#" (a legacy code in my case), the reason is that the property -href will have value location + "#", which will cause the code in global handler ignore-anchor-click to work (https://github.com/metosin/reitit/blob/master/modules/reitit-frontend/src/reitit/frontend/history.cljs#L89-L101). You probaly want to know this, if you plan to use controller with :identity that is always different and there are anchors with :href "#" in your code.