Add a new FAQ

This commit is contained in:
Mike Thompson 2017-08-05 15:30:13 +10:00
parent e7114d1a6c
commit 906ab9c885
3 changed files with 52 additions and 2 deletions

View File

@ -7,7 +7,8 @@
- added [a new mental model](/docs/MentalModelOmnibus.md#on-dsls-and-machines)
- added [a new FAQ entry](/docs/FAQs/When-Does-Dispatch-Happen.md) on dispatch processing
- added [a new FAQ entry](/docs/FAQs/DB_Normalisation.md) on representing normalised data in `app-db`.
- added [a new FAQ entry](/docs/FAQs/GlobalInterceptors.md) on how to register a global interceptor.
#### Breaking
- [#357](https://github.com/Day8/re-frame/pull/357)

View File

@ -0,0 +1,49 @@
### Question
Does re-frame allow me to register global interceptors? Ones which are included
for every event handler?
### Short Answer
No, nothing direct.
### Longer Answer
It's easy to make happen.
Let's assume you have an interceptor called `omni-ceptor` which you want
automatically added to all your event handlers.
You'd write a replacement for both `reg-event-db` and `reg-event-fx`, and get
these replacements to automatically add `omni-ceptor` to the interceptor
chain at registration time.
Here's how to write one of these auto-injecting replacements:
```clj
(defn my-reg-event-db ;; a replacement for reg-event-db
;; 2-arity with no interceptors
([id handler]
(my-reg-event-db id nil handler))
;; 3-arity with interceptors
([id interceptors handler]
(re-frame.core/reg-event-db ;; which uses reg-event-db
id
[omni-ceptor interceptors] ;; <-- inject `omni-ceptor`
handler)))
```
NB: did you know that interceptor chains are flattened and nils are removed?
With this in place, you would always use `my-reg-event-db`
instead of the standard `reg-event-db`:
```clj
(my-reg-event-db
:event-id
(fn [db v]
...))
```
And, hey presto, you'd have your `omni-ceptor` "globally" injected.

View File

@ -9,7 +9,7 @@
5. [Why do we need to clear the subscription cache when reloading with Figwheel?](Why-Clear-Sub-Cache.md)
6. [How can I detect exceptions in Event Handlers?](CatchingEventExceptions.md)
7. [How do I store normalised data in app-db?](DB_Normalisation.md)
8. [How do I register a global interceptor](GlobalInterceptors.md)
## Want To Add An FAQ?