UNPKG

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