1 | Sttore
|
2 | =========
|
3 |
|
4 | > State Manager
|
5 |
|
6 | ```
|
7 | npm install sttore
|
8 | ```
|
9 | ## Use
|
10 |
|
11 | ```js
|
12 | import sttore from 'sttore'
|
13 | ```
|
14 | or
|
15 | ```js
|
16 | const sttore = require('sttore')
|
17 | ```
|
18 |
|
19 |
|
20 | ```js
|
21 | const store = sttore({
|
22 | name: 'Miguel',
|
23 | age: 24
|
24 | })
|
25 |
|
26 | store() // { name: 'Miguel', age: 24 }
|
27 | ```
|
28 |
|
29 | ## Update
|
30 |
|
31 | ```js
|
32 | const store = sttore({
|
33 | name: 'Miguel',
|
34 | age: 24
|
35 | })
|
36 |
|
37 | store({ ...store(), age: 29 }) // { name: 'Miguel', age: 29 }
|
38 | ```
|
39 |
|
40 | or use set
|
41 |
|
42 | ```js
|
43 | store.set('name', 'Juan') // true
|
44 | store() // { name: 'Juan', age: 29 }
|
45 | ```
|
46 |
|
47 | ## Changes
|
48 |
|
49 | Identify the states that had a change.
|
50 |
|
51 | ```js
|
52 | const store = sttore({
|
53 | name: 'María',
|
54 | age: 18
|
55 | })
|
56 |
|
57 | store.set('age', 15)
|
58 | store.change('age') // true
|
59 | store.change() // true
|
60 | store.change('name') // false
|
61 | ```
|
62 |
|
63 | You can get only the states that had change.
|
64 |
|
65 | ```js
|
66 | store.changes() // { age: 15 }
|
67 | ```
|
68 |
|
69 | ## Pending
|
70 |
|
71 | They are updates that are pending confirmation.
|
72 |
|
73 | A pending data cannot be taken as a change, for this it is required to be confirmed.
|
74 | For an update into pending mode, it should be passed as the third parameter a `true`.
|
75 |
|
76 | ```js
|
77 | const store = sttore({
|
78 | name: 'María',
|
79 | age: 18
|
80 | })
|
81 | store.set('name', 'Ivan', true)
|
82 | store.set('age', 22)
|
83 | store.changes() // { age: 22 }
|
84 | ```
|
85 |
|
86 | To confirm the update use the `confirm` method or use the `cancel` method to reject
|
87 |
|
88 | ```js
|
89 | store.confirm('name') // true
|
90 | store.changes() // { name: 'Ivan', age: 22 }
|
91 | ```
|
92 | or cancel
|
93 | ```js
|
94 | store.cancel('name') // true
|
95 | store.changes() // { age: 22 }
|
96 | ```
|
97 |
|
98 | You can confirm or cancel all.
|
99 |
|
100 | ```js
|
101 | states.confirm()
|
102 | // or
|
103 | store.cancel()
|
104 | ```
|
105 | ## Helper
|
106 |
|
107 | The helpers are just an extra helpful information. This can be used for validation messages for example.
|
108 |
|
109 | ```js
|
110 | store.helper('name', 'This name is required') // 'This name is required'
|
111 | store.helper('name') // 'This name is required'
|
112 | store.helpers() // { name: 'This name is required', age: '' }
|
113 | store.helpers({ name: '', age: 'Its not numeric' }) // { name: '', age: 'Its not numeric' }
|
114 | ```
|
115 |
|
116 | Helpers are originated by states, by default their value is an empty string.
|
117 |
|
118 | ## Restore
|
119 |
|
120 | You can restore the `helpers` or cancel all `pending` data or both.
|
121 |
|
122 | ```js
|
123 | // restore both
|
124 | store.restore()
|
125 | // cancel all update pending
|
126 | store.restore('pending')
|
127 | // restore helper
|
128 | store.restore('helper')
|
129 | ```
|
130 |
|
131 | ## Initial
|
132 |
|
133 | Initial is in charge of initializing the state.
|
134 |
|
135 | ```js
|
136 | const store = sttore({
|
137 | name: 'María',
|
138 | age: 18
|
139 | })
|
140 |
|
141 | store.set('name', 'Leo')
|
142 | store.set('age', 24)
|
143 | store.change() // true
|
144 | store() // { name: 'Leo', age: 24 }
|
145 |
|
146 | store.init()
|
147 | store() // { name: 'María', age: 18 }
|
148 | store.change() // false
|
149 | ```
|
150 |
|
151 | You can specify an initial state for it.
|
152 |
|
153 | ```js
|
154 | store.init(store)
|
155 | store() // { name: 'Leo', age: 24 }
|
156 | store.change() // false
|
157 | ```
|
158 |
|
159 | ## Nesting
|
160 |
|
161 | It is possible to nest several stores.
|
162 |
|
163 | ```js
|
164 | const store = sttore({
|
165 | date: '12/12/12',
|
166 | contact: sttore({
|
167 | name: 'Diana',
|
168 | phone: 123456789
|
169 | })
|
170 | })
|
171 |
|
172 | store().contact().name // Diana
|
173 | store().contact().phone // 123456789
|
174 | store().contact.set('name', 'Raúl')
|
175 | store().contact().name // Raúl
|
176 | store().contact.change('name') // true
|
177 | store().contact.change('phone') // false
|
178 | store.change() // true
|
179 | ```
|