Update controller docs

This commit is contained in:
Juho Teperi 2018-12-04 15:02:16 +02:00
parent 36109f142c
commit 9e30836839
2 changed files with 21 additions and 18 deletions

View file

@ -9,32 +9,34 @@ Controllers run code when a route is entered and left. This can be useful to:
## How controllers work ## How controllers work
A controller consists of three functions: A controller map can contain these properties:
* `params` which takes a Match and returns an arbitrary value. * `identity` function which takes a Match and returns an arbitrary value,
* `start` which takes the result of params and whose return value is discarded. * or `parameters` value, which declares which parameters should affect
* `stop` which takes the result of params and whose return value is discarded. controller identity
* `start` & `stop` functions, which are called with controller identity
When you navigate to a route that has a controller, `params` gets called first When you navigate to a route that has a controller, controller identity
and then `start` is called with its return value. When you exit that route, is first resolved by calling `identity` function, or by using `parameters`
`stop` is called with the return value of `params.` declaration, or if neither is set, the identity is `nil`. Next controller
is initialized by calling `start` is called with the identity value.
When you exit that route, `stop` is called with the return value of `params.`
If you navigate to the same route with different parameters, `params` gets If you navigate to the same route with different match, identity gets
called again. If the return value changes from the previous return value, `stop` resolved again. If the identity changes from the previous value, controller
and `start` get called again. is reinitialized: `stop` and `start` get called again.
You can add controllers to a route by adding them to the route data in the You can add controllers to a route by adding them to the route data in the
`:controllers` vector. For example: `:controllers` vector. For example:
```cljs ```cljs
["/item/:id" ["/item/:id"
{:controllers [{:params (fn [match] (get-in match [:path-params :id])) {:controllers [{:parameters {:path [:id]}
:start (fn [item-id] (js/console.log :start item-id)) :start (fn [parameters] (js/console.log :start (-> parameters :path :id)))
:stop (fn [item-id] (js/console.log :stop item-id))}]}] :stop (fn [parameters] (js/console.log :stop (-> parameters :path :id)))}]}]
``` ```
If you leave out `params`, `start` and `stop` get called with `nil`. You can You can leave out `start` or `stop` if you do not need both of them.
leave out `start` or `stop` if you do not need both of them.
## Enabling controllers ## Enabling controllers

View file

@ -7,8 +7,9 @@
(defn get-identity (defn get-identity
"Get controller identity given controller and match. "Get controller identity given controller and match.
To select interesting properties from Match :parameters option can be used. To select interesting properties from Match :parameters option can be set.
Resulting value is map of param-type => param => value. Value should be param-type => [param-key]
Resulting value is map of param-type => param-key => value.
For other uses, :identity option can be used to provide function from For other uses, :identity option can be used to provide function from
Match to identity. Match to identity.
@ -35,7 +36,7 @@
(defn apply-controller (defn apply-controller
"Run side-effects (:start or :stop) for controller. "Run side-effects (:start or :stop) for controller.
The side-effect function is called with controller params." The side-effect function is called with controller identity value."
[controller method] [controller method]
(when-let [f (get controller method)] (when-let [f (get controller method)]
(f (::identity controller)))) (f (::identity controller))))