Skip to content

Commit a0b676a

Browse files
committed
add additional info in the readme and increase the version for 'cur'
1 parent 36dc045 commit a0b676a

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

README.md

+82-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,93 @@
1-
reagent-cursor
1+
Cursors
22
==============
33

4-
Optional cursors library for Reagent.
4+
Cursors can be seen as a kind of *pointers* to a particular part of an
5+
atom, which behaving exactly like a normal atom. This means that you
6+
use the same functions you would on an atom (`reset!`, `swap!`,
7+
`deref`, `add-watch`, etc) but affect only the part you are interested
8+
in.
9+
10+
This enables you to create reusable functions and components by
11+
abstracting away complex paths and getter/setter functions.
12+
13+
```clj
14+
;; what was...
15+
(swap! my-atom update-in [:some :path :that :might :be :quite :deep] my-fn)
16+
17+
;; ...can now become
18+
19+
(swap! my-cursor my-fn)
20+
21+
;; Notice that the path is no longer hardcoded; it could be a simple
22+
;; atom, or a cursor pointing to the 10th level of a complex nested
23+
;; hashmap.
24+
25+
;; How about associating a value into the nested structure? No
26+
;; problem! Just `reset!` the cursor:
27+
28+
(reset! my-cursor "my-new-value")
29+
30+
;; Now just deref it:
31+
32+
@my-cursor
33+
34+
=> "my-new-value"
35+
36+
```
537

638
Usage
739
-----
840

9-
Add `[reagent/reagent-cursor "0.1.1"]` to `:dependencies` in `project.clj`.
41+
Add `[reagent/reagent-cursor "0.1.2"]` to `:dependencies` in `project.clj`.
42+
43+
In your Reagent application `(:require [reagent.cursor :as rc])`.
44+
45+
There is two main functions available to create cursors: `cursor` and `cur`.
46+
47+
## cursor
48+
49+
`cursor` has two arities.
50+
51+
When given a single argument (a path), it returns a function that can
52+
create a cursor when given an atom. *Useful to create mutliple cursors
53+
with the same path.*
54+
55+
56+
```clj
57+
(def my-custom-cursor-fn (rc/cursor [:some :arbitrary :path]))
58+
59+
(map my-custom-cursor-fn [atom1 atom2 atom3])
60+
61+
;; this will return a collection of [cursor1 cursor2 cursor3]
62+
```
63+
64+
When given two arguments, `cursor` will return a cursor.
65+
66+
```clj
67+
(def c1 (rc/cursor [:some :arbitrary :path] atom1))
68+
69+
```
70+
71+
72+
## cur
73+
74+
75+
`cur` is the little brother of `cursor`. It will *only* accept 2
76+
arguments (the atom and a path), but is guaranteed to return a
77+
cursor.
78+
79+
Note that the atom argument is placed on the left, allowing
80+
you to use a threading macro.
1081

11-
In your Reagent application, `(:require [reagent.cursor :as rc])` and then construct cursors from Reagent atoms like this `(rc/cursor [:path :to :data] ratom)`.
82+
```clj
1283

13-
See docstrings in `reagent.cursor` for more details.
84+
(-> my-atom
85+
(rc/cur [:some :path]) ;; <---- create the cursor
86+
(add-watch :my-watch #(println "updated!"))
87+
(historian/record! :my-state)
88+
(ls/local-storage :my-state))
89+
90+
```
1491

1592
License
1693
-------

project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject reagent/reagent-cursor "0.1.1"
1+
(defproject reagent/reagent-cursor "0.1.2"
22
:description "Provide Om-inspired cursors for Reagent"
33
:url "http://github.com/reagent-project/reagent-cursor"
44
:license {:name "Eclipse Public License"

0 commit comments

Comments
 (0)