re-frame/docs/API.md

116 lines
3.3 KiB
Markdown
Raw Normal View History

2017-07-20 15:14:07 +00:00
## The re-frame API
2017-07-23 03:09:38 +00:00
Orientation:
2017-07-24 12:50:55 +00:00
1. The API is provided by `re-frame.core`:
- at some point, it would be worth your time [to browse it](/src/re_frame/core.cljc)
- to use re-frame, you'll need to `require` it, perhaps like this ...
```clj
2017-07-24 03:33:40 +00:00
(ns my.namespace
(:require [re-frame.core :as rf]))
2017-07-24 12:50:55 +00:00
... now use rf/reg-event-fx or rf/subscribe
2017-07-24 03:33:40 +00:00
```
2017-07-24 12:50:55 +00:00
2. The API is small. Writing an app, you use less than 10 API functions. Maybe just 5.
2017-07-24 03:33:40 +00:00
3. There's no auto-generated docs [because of this problem](/src/re_frame/core.cljc#L23-L36)
but, as a substitute,
2017-07-24 12:50:55 +00:00
the links below take you to the doc-strings of often-used API functions.
2017-07-20 15:01:51 +00:00
2017-10-30 11:26:25 +00:00
## Doc Strings For The Significant API Functions
2017-07-20 15:01:51 +00:00
The core API consists of:
2017-07-24 03:52:52 +00:00
- [dispatch](/src/re_frame/router.cljc#L229-L239) or [dispatch-sync](/src/re_frame/router.cljc#L247-L259).
- [reg-event-db](/src/re_frame/core.cljc#L71-L80) or [reg-event-fx](/src/re_frame/core.cljc#L87-L97)
2017-07-25 22:42:33 +00:00
- [reg-sub](/src/re_frame/subs.cljc#L176-L261) and [subscribe](/src/re_frame/subs.cljc#L67-L107) working together
2017-07-23 03:09:38 +00:00
Occasionally, you'll need to use:
2017-07-24 03:33:40 +00:00
- [reg-fx](/src/re_frame/fx.cljc#L17-L40)
2017-07-24 03:52:52 +00:00
- [reg-cofx](/src/re_frame/cofx.cljc#L14-L22) and [inject-cofx](/src/re_frame/cofx.cljc#L29-L80) working together
2017-07-20 15:01:51 +00:00
2017-07-24 03:52:52 +00:00
And, finally, there are the builtin Interceptors:
2017-07-29 03:26:41 +00:00
- [path](/src/re_frame/std_interceptors.cljc#L152-L176)
- [after](/src/re_frame/std_interceptors.cljc#L264-L274)
- [debug](/src/re_frame/std_interceptors.cljc#L13-L38)
2017-07-24 03:52:52 +00:00
- and browse [the others](/src/re_frame/std_interceptors.cljc)
2017-07-20 15:01:51 +00:00
## Built-in Effect Handlers
2017-07-24 12:50:55 +00:00
The following built-in effects are also a part of the API:
2017-07-24 12:50:55 +00:00
#### :dispatch-later
`dispatch` one or more events after given delays. Expects a collection
of maps with two keys: `:ms` and `:dispatch`
usage:
```clj
{:dispatch-later [{:ms 200 :dispatch [:event-id "param"]}
{:ms 100 :dispatch [:also :this :in :100ms]}]}
```
Which means: in 200ms do this: `(dispatch [:event-id "param"])` and in 100ms ...
2017-07-24 12:50:55 +00:00
***
#### :dispatch
`dispatch` one event. Expects a single vector.
usage:
```clj
{:dispatch [:event-id "param"] }
```
2017-07-24 12:50:55 +00:00
***
#### :dispatch-n
2017-10-30 11:26:25 +00:00
`dispatch` more than one event. Expects a collection events.
usage:
```clj
{:dispatch-n (list [:do :all] [:three :of] [:these])}
```
2017-10-30 11:26:25 +00:00
Note 1: The events supplied will be dispatched in the order provided.
Note 2: nil events are ignored which means events can be added
2017-07-24 12:50:55 +00:00
conditionally:
```clj
{:dispatch-n (list (when (> 3 5) [:conditioned-out])
[:another-one])}
```
2017-10-30 11:26:25 +00:00
2017-07-24 12:50:55 +00:00
***
#### :deregister-event-handler
2017-10-30 11:26:25 +00:00
removes a previously registered event handler. Expects either a single id
(typically a keyword), or a seq of ids.
usage:
```clj
{:deregister-event-handler :my-id)}
```
or:
```clj
{:deregister-event-handler [:one-id :another-id]}
```
2017-07-24 12:50:55 +00:00
***
#### :db
reset! app-db with a new value. Expects a map.
usage:
```clj
{:db {:key1 value1 :key2 value2}}
```
***
2017-07-20 15:01:51 +00:00
Previous: [First Code Walk-Through](CodeWalkthrough.md)      
2017-07-20 15:01:51 +00:00
Up: [Index](README.md)      
Next: [Mental Model Omnibus](MentalModelOmnibus.md)
2017-07-20 15:01:51 +00:00
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
<!-- END doctoc generated TOC please keep comment here to allow auto update -->