Inge Solvoll's blog

Menu

  • Home
  • Archives
  • Tags
  • About me
  • RSS
June 18, 2018

Kee-frame controller tricks

The power of controllers

The controller rules, taken from keechma, are centered around pure route data. That's really powerful, as you can use mostly plain Clojure to get what you want from your controllers. Often the solution is dead simple, but it's not always easy to spot. This is a guide to the most useful tricks, it will be expanded as new ones appear!

How to trigger an event once, at startup

So you want something to happen only once, but immediately. Things like:

  • Fetching initial data from the server
  • Start a polling loop
  • Letting the user know we are loaded and ready to go
  • Forcing the user to express her never ending love for cookies.

What you need is a :params function that triggers start when invoked for the first time, and then always returns the same result as the first invocation. That sounds like a plain Clojure function that we all know:

{:params (constantly true) ;; true, or whatever non-nil value you prefer
 :start  [:call-me-once-then-never-again]}

How to restart a controller on every route change

Maybe you want to store a trail of breadcrumbs, maybe you want some logging done. Either way, this one is also quite simple.

For this case you need a :params function that returns a new unique result for every new unique route data. What tiny but familiar Clojure function could we use to achieve that?

{:params identity
 :start  [:log-user-activity]}

Getting weird

As you can see, most things with controllers are quite simple and use plain Clojure. Just for fun, let's have a look at a controller that triggers randomly:

{:params #(rand-nth [nil % % %])
 :start  [:will-receive-the-route-data-quite-often-but-not-always]}

That's it for now. Please report your useful tricks at kee-frame issues and I'll add them here!


Tags: re-frame kee-frame clojurescript

« Revisited: How to use a charting library in reagent Kee-frame: Putting the URL in charge »

Copyright © 2021 Inge Solvoll

Powered by Cryogen | Free Website Template by Download Website Templates