@@ -12,8 +12,8 @@ WIP docs!
12
12
:::
13
13
14
14
A Collection holds a set of Information that we need to remember at a later point in time.
15
- It is designed for arrays of data objects following the same structure .
16
- Be aware that each collected Data needs an ** unique primaryKey** to properly identify it later.
15
+ It is designed for arrays of data objects following the same pattern .
16
+ Be aware that each collected Data needs an ** unique primaryKey** to get properly identified later.
17
17
We instantiate a Collection with help of an [ Agile Instance] ( ../agile-instance/Introduction.md ) here called ` App ` .
18
18
By doing this the Collection gets automatically bound to the Agile Instance it was created from.
19
19
``` ts
@@ -25,11 +25,11 @@ but there we have to pass the `Agile Instance`, to which the Collection should g
25
25
const MY_COLLECTION = new Collection (App );
26
26
```
27
27
Both instantiations lead to the same result, but we recommend using the former way.
28
- After we have successfully created our Collection, we can dynamically manipulate and work with it.
28
+ After we have successfully created our Collection, we can work with it dynamically and easily .
29
29
``` ts
30
30
MY_COLLECTION .collect ({id: 1 , name: " jeff" }); // Add Item to Collection
31
- MY_COLLECTION .remove (1 ).everywhere (); // Remove Item from everywhere
32
- MY_COLLECTION .persist (); // Persists Collection Value into the Storage
31
+ MY_COLLECTION .remove (1 ).everywhere (); // Remove Item from Collection
32
+ MY_COLLECTION .persist (); // Persists Collection Value into a Storage
33
33
```
34
34
Most methods we use to modify, mutate and access the Collection are chainable.
35
35
``` ts
@@ -38,36 +38,48 @@ MY_COLLECTION.collect({id: 1, name: "jeff"}).persist().removeGroup('myGroup').re
38
38
39
39
## 🔹 Item
40
40
41
- Each Data we have added to the Collection, like with the ` collect ` method,
42
- gets applied to an Item and stored as such in our Collection.
41
+ Each Data Object we add to our Collection, for example with the ` collect ` method,
42
+ gets transformed to an Item. This Item than gets stored in our Collection.
43
+ We can simply access each Item with the ` getItem ` method and the correct primary Key.
43
44
``` ts
44
45
MY_COLLECTION .getItem (/* primary Key */ ); // Returns Item at the primary Key
45
46
```
46
- An Item is an extension of the State Class and offers the same powerful features.
47
+ The cool thing about Items is, they are an extension of the ` State Class ` .
48
+ This means that they have the same powerful tools like a State.
47
49
``` ts
48
- const myItem = MY_COLLECTION .getItem (1 );
50
+ MY_COLLECTION .collect ({id: 1 , name: " jeff" }); // Collect Data
51
+ const myItem = MY_COLLECTION .getItem (1 ); // Returns Item at primaryKey '1'
52
+ myItem .value ; // Returns '{id: 1, name: "jeff"}'
49
53
myItem .patch ({name: " frank" }); // Update property 'name' in Item
54
+ myItem .undo (); // Undo latest change
50
55
```
51
56
52
57
## 👨👧👦 [ Group] ( ./group/Introduction.md )
53
58
54
59
Often applications need to categorize and preserve ordering of structured data and
55
- in AgileTs Groups are the cleanest way to reach this goal. They allow us to
60
+ in AgileTs Groups are the right way to reach this goal. They allow us to
56
61
cluster together data from a Collection as an array of primary Keys.
62
+ ``` ts
63
+ MY_COLLECTION .createGroup (" groupName" , [/* initial Items*/ ]);
64
+ ```
57
65
We might use a Group, if we want to have an array of 'Today Todos' from
58
66
a Todo Collection or Posts that belong to the logged-in User from the Post Collection.
59
67
``` ts
60
- MY_COLLECTION .createGroup (" groupName" , [/* initial Items*/ ]);
68
+ USERS .collect (user );
69
+ POSTS .collect (user .posts , user .id );
61
70
```
62
- We are able to create as many Groups as we want, and the Collection won't lose
63
- its redundant behaviour, since the Items are still stored in the Collection, and
64
- the Groups are only like an interface to it.
71
+ Here we have two Collections, one for users and another for posts.
72
+ We can collect posts specific to a user and group them automatically by the user's id.
73
+
74
+ In our Collection we are able to create as many Groups as we want, and the Collection won't lose
75
+ its redundant behaviour. This is due to the fact, that each Item gets stored in the Collection itself and not in the Group.
76
+ You can imagine a Group like an interface to the Collection Data.
65
77
``` ts
66
78
MY_COLLECTION .createGroup (" group1" , [1 , 2 , 3 ]);
67
79
MY_COLLECTION .createGroup (" group2" , [2 , 5 , 8 ]);
68
80
MY_COLLECTION .createGroup (" group5000" , [1 , 10 , 500 , 5 ]);
69
81
```
70
- A Group is an extension of the State Class and offers the same powerful features.
82
+ Also, a Group is an extension of the ` State Class ` and offers the same powerful features.
71
83
``` ts
72
84
MY_STATE .undo (); // Undo latest change
73
85
MY_GROUP .reset (); // Reset Group to its intial Value
@@ -77,25 +89,36 @@ But be aware that the `value` might not be the output you expect.
77
89
``` ts
78
90
MY_GROUP .value ; // Returns '[8, 5, 30, 1]'
79
91
```
80
- It holds the primary Keys of the Items the Group represent .
81
- To get the right value to the primary Keys just use ` output ` property.
92
+ Because this property doesn't hold the Item Values, it contains the primary Keys the Group represents .
93
+ To get the Item Value to each primary Keys, just use the ` output ` property.
82
94
``` ts
83
95
MY_GROUP .output ; // Returns '[{ id: 8, name: 'jeff' }, ...]'
84
96
```
85
97
86
98
## 🔮 [ Selector] ( ./selector/Introduction.md )
87
99
88
- Selectors allow us to _ select_ an Item from a Collection.
89
- We might use the Selector, if we want to select a 'current User' from our User Collection or
90
- the 'current viewing Post' from our Post Collection.
100
+ Selectors allow us to _ select_ one specific Item from our Collection.
91
101
``` ts
92
- MY_COLLECTION .createGroup (" selectorName" , /* to select Item Key*/ );
102
+ MY_COLLECTION .createSelector (" selectorName" , /* to select Item Key*/ );
93
103
```
94
- A Selector is an extension of the State Class and offers the same powerful features .
104
+ We might use the Selector, if we want to select the 'current logged-in User' from our User Collection .
95
105
``` ts
96
- MY_STATE . undo (); // Undo latest change
106
+ USERS . select ( /* current logged-in userId */ );
97
107
```
98
- But be aware that by mutating the Selector we won't modify the
108
+ <br />
109
+
110
+ A Selector is also able to select a not existing Item, then it holds
111
+ a reference to this Item. But be aware that the Value of the Selector is
112
+ ` undefined ` during this period of time, since AgileTs doesn't know your desired Item.
113
+ ``` ts
114
+ MY_SELECTOR .select (" notExistingItem" );
115
+ MY_SELECTOR .value ; // Returns 'undefined' until it the Item got added to the Collection
116
+ ```
117
+ A Selector is an extension of the State Class too and offers the same powerful features.
118
+ ``` ts
119
+ MY_SELECTOR .undo (); // Undo latest change
120
+ ```
121
+ But be aware that by mutating the Selector Value we won't modify the
99
122
selected Item in the Collection. To do that we have to modify the Item directly.
100
123
``` ts
101
124
MY_SELECTOR .item .set ({id: 1 , name: " jeff" });
@@ -105,18 +128,18 @@ MY_SELECTOR.item.set({id: 1, name: "jeff"});
105
128
106
129
There are two ways to configure our Collection:
107
130
108
- - 1 . The plain _ object_ way, where we configure everything in an object.
131
+ - ** 1. ** The plain _ object_ way, where we configure everything in an object.
109
132
Here we are limited in the creation of Groups and Selectors,
110
133
because we can't create them on our own. The Collection takes care of it instead,
111
- which limits us in configuring of these Instances.
134
+ which limits us in configuring these Instances.
112
135
``` ts
113
136
const Collection = App .createCollection ({
114
137
key: ' dummyCollection' ,
115
138
group: [" dummyGroup" ]
116
139
})
117
140
```
118
141
119
- - 2. The _function_ way , where we configure everything in an object too .
142
+ - ** 2. ** The _function_ way , where we configure everything in an object too .
120
143
But this time the object has to be returned by a function , which has the collection as its only parameter.
121
144
By approaching the collection, we are able to create Groups and Selectors on our own, which
122
145
gives us more freedom in configuring these Instances.
@@ -129,8 +152,8 @@ There are two ways to configure our Collection:
129
152
}))
130
153
```
131
154
132
- Here is a Typescript Interface for quick reference , however
133
- each property will be explained in more detail below .
155
+ Here is a Typescript Interface of the configuration Object for quick reference ,
156
+ however each property will be explained in more detail below .
134
157
` ` ` ts
135
158
export interface CreateCollectionConfigInterface<DataType = DefaultItem> {
136
159
groups?: { [key: string]: Group<any> } | string[];
@@ -142,6 +165,39 @@ export interface CreateCollectionConfigInterface<DataType = DefaultItem> {
142
165
}
143
166
` ` `
144
167
168
+ ### ` groups `
169
+ Here we define the initial [Groups ](#groups ) of our Collection .
170
+ We have two options to add them .
171
+ The first way is to just pass an Array of Group Names , than
172
+ AgileTs creates these Groups for us .
173
+ ` ` ` ts
174
+ const MY_COLLECTION = App.createCollection({
175
+ groups: ["myGroup1", "myGroup2"]
176
+ });
177
+ ` ` `
178
+ But if we want to add some initial Items to the Groups we have to go the
179
+ ` function ` config way .
180
+ ` ` ` ts
181
+ const MY_COLLECTION = App.createCollection((collection) => ({
182
+ key: 'dummyCollection',
183
+ group: {
184
+ myGroup1: collection.Group(["item1", "item2"]),
185
+ myGroup2: collection.Group(["item5", "item2", "item6"])
186
+ }
187
+ }))
188
+ ` ` `
189
+
190
+ ### ` key `
191
+ The Key / Name is an optional property , that gets used to identify our Collection .
192
+ This is pretty useful during debug sessions or if we persist our Collection ,
193
+ where it automatically uses the ` key ` as persist key .
194
+ We recommend giving each Collection an unique ` key ` . It has only advantages .
195
+ ` ` ` ts
196
+ const MY_COLLECTION = App.createCollection({
197
+ key: "myKey"
198
+ });
199
+ ` ` `
200
+
145
201
146
202
## 🟦 Typescript
147
203
@@ -154,4 +210,5 @@ interface UserInterface {
154
210
const MY_COLLECTION = App .createState <UserInterface >();
155
211
MY_COLLECTION .collect ({id: " invalidType" , animal: " Lion" }); // Error
156
212
MY_COLLECTION .collect ({id: 1 , name: " hans" }); // Success
157
- ```
213
+ ```
214
+ This type defines the Value Type of the Collection Items.
0 commit comments