Skip to content

Commit 06bc192

Browse files
author
Vlad Balin
committed
collection docs
1 parent 67742f1 commit 06bc192

File tree

4 files changed

+110
-113
lines changed

4 files changed

+110
-113
lines changed

docs/chapters/Events.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class MyMessenger extends Messenger {
116116
}
117117
```
118118

119-
### `implements` Events
119+
### Events mixin methods (7)
120120

121121
Messenger implements [Events](#events-mixin) mixin.
122122

docs/chapters/Mixins.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Mixins
22

3-
## Decorators
4-
53
Type-R is based on mixins with configurable properties merge rules.
64

5+
## Definition
6+
77
### `decorator` @mixins( mixinA, mixinB, ... ) class X ...
88

99
Merge specified mixins to the class definition. Both plain JS object and class constructor may be used as mixin. In the case of the class constructor, missing static members will copied over as well.

docs/chapters/collection.md

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# Collection
22

3-
Collections are ordered sets of records. You can bind "change" events to be notified when any record in the collection has been modified, listen for "add" and "remove" events, fetch the collection from the server, and use a full suite of Underscore.js methods.
4-
5-
Collection is implicitly defined for every record with a constructor accessible as `MyRecord.Collection`. In most cases, you
6-
don't need to declare it manually.
3+
Collections are ordered sets of records. You can bind "changes" events to be notified when the collection has been modified, listen for "add" and "remove" events, fetch the collection from the server, and use a full suite of iteration methods.
74

85
```javascript
96
// Implicitly defined collection.
@@ -30,17 +27,19 @@ class Comics extends Book {
3027
}
3128
```
3229

33-
## Collection definition
30+
## Definition
31+
32+
### RecordClass.Collection
3433

35-
### RecordConstructor.Collection
34+
Default collection constructor for the given Record class.
3635

37-
Every `Record` class has implicitly defined Collection, which can be referenced adding the `.Collection` to the record's constructor.
36+
Collection is implicitly defined for every record with a constructor accessible as `MyRecord.Collection`. In most cases, you don't need to declare it manually.
3837

39-
### (record definition) `static` Collection = CollectionConstructor
38+
### CollectionClass.Refs
4039

41-
Replaces implicitly defined collection with externally defined collection class.
40+
Non-aggregating collection constructor.
4241

43-
### (collection definition) `static` model = RecordConstructor
42+
### `static` model = RecordConstructor
4443

4544
Specify the record type inside of the collection's definition. This property is being set automatically for collection types referenced as `MyRecord.Collection`.
4645

@@ -50,7 +49,7 @@ Specify the record type inside of the collection's definition. This property is
5049
}
5150
```
5251

53-
## Create and access the collection
52+
## Create and dispose
5453

5554
### new Collection()
5655

@@ -68,6 +67,19 @@ var tabs = new TabSet([tab1, tab2, tab3]);
6867

6968
Initialization function which is called at the end of the constructor.
7069

70+
### collection.dispose()
71+
72+
### collection.clone()
73+
74+
### collection.createSubset()
75+
76+
## Read and Update
77+
78+
Methods to update the collection. They accept common options:
79+
80+
- `sort : false` - do not sort the collection.
81+
- `parse : true` - parse raw JSON (used to set collection with a data from the server).
82+
7183
### collection.get( id )
7284
Get a record from a collection, specified by an `id`, a `cid`, or by passing in a record.
7385

@@ -86,13 +98,6 @@ Like an array, a Collection maintains a length property, counting the number of
8698

8799
Raw access to the JavaScript array of records inside of the collection. Usually you'll want to use `get`, `at`, or the other methods to access record objects, but occasionally a direct reference to the array is desired.
88100

89-
## Updates
90-
91-
Methods to update the collection. They accept common options:
92-
93-
- `sort : false` - do not sort the collection.
94-
- `parse : true` - parse raw JSON (used to set collection with a data from the server).
95-
96101
### collection.add( records, options? )
97102

98103
Add a record (or an array of records) to the collection. If this is the `Record.Collection`, you may also pass raw attributes objects, and have them be vivified as instances of the `Record`. Returns the added (or preexisting, if duplicate) records.
@@ -149,22 +154,44 @@ Calling `collection.reset()` without passing any records as arguments will empty
149154
1. Trigger event `reset`( collection, options ).
150155
2. Trigger event `changes`( collection, options ).
151156

152-
## Transactions
157+
### collection.transaction( fun )
158+
159+
Execute the sequence of updates in `fun` function in the scope of the transaction.
153160

154161
All collection updates occurs in the scope of transactions. Transaction is the sequence of changes which results in a single `changes` event.
155162

156163
Transaction can be opened either manually or implicitly with calling any of collection update methods.
157164
Any additional changes made to the collection or its items in event handlers will be executed in the scope of the original transaction, and won't trigger an additional `changes` events.
158165

159-
### collection.transaction( fun )
160-
161-
Execute the sequence of updates in `fun` function in the scope of the transaction.
162-
163166
### collection.updateEach( iteratee : ( val : Record, index ) => void, context? )
164167

165168
Similar to the `collection.each`, but wraps the loop in a transaction.
166169

167-
## Observable changes
170+
### collection.push( record, options? )
171+
172+
Add a record at the end of a collection. Takes the same options as add.
173+
174+
### collection.pop( options? )
175+
Remove and return the last record from a collection. Takes the same options as remove.
176+
177+
### collection.unshift( record, options? )
178+
179+
Add a record at the beginning of a collection. Takes the same options as add.
180+
181+
### collection.shift( options? )
182+
Remove and return the first record from a collection. Takes the same options as remove.
183+
184+
### collection.slice( begin, end )
185+
186+
Return a shallow copy of the `collection.models`, using the same options as native Array#slice.
187+
188+
### collection.indexOf( recordOrId : any ) : number
189+
190+
Return an index of the record in the collection, and -1 if there are no such a record in the collection.
191+
192+
Can take the record itself as an argument, `id`, or `cid` of the record.
193+
194+
## Change events
168195

169196
Object tree formed by nested records is deeply observable by default; changes in every item trigger change events for the collection and all parent elements in sequence.
170197

@@ -178,31 +205,22 @@ Collection triggers following events on change:
178205
- `reset` *( collection, options )* if `collection.reset()` was used to update the collection.
179206
- `changes` *( collection, options )* in case of any changes.
180207

181-
## Listening to changes with Events API
182-
183-
[Events API](../10_Events.md) is used for managing events subscriptions.
184-
185-
### listener.listenTo( record, event, handler )
186-
187-
[Events API](../10_Events.md) method used to listen to the any of the change events.
188-
189-
### listener.stopListening( record )
190-
191-
[Events API](../10_Events.md) method to explicitly stop all event subscriptions from the record.
208+
### Events mixin methods (7)
192209

193-
Not needed if the listener is other record or collection.
194-
195-
## Listening to item events in the collection
210+
Collection implements [Events](#events-mixin) mixin.
196211

197212
### `static` itemEvents = { eventName : `handler`, ... }
198213

199214
Subscribe for events from records. The `hander` is either the collection's method name, the handler function, or `true`.
200215

201216
When `true` is passed as a handler, the corresponding event will be triggered on the collection.
202217

203-
## map and forEach
218+
## Iteration
204219

205220
### collection.forEach( iteratee : ( val : Record, index ) => void, context? )
221+
222+
Same as `collection.each()`.
223+
206224
### collection.each( iteratee : ( val : Record, index ) => void, context? )
207225

208226
Iterate through the elements of the collection. Similar to `Array.forEach`.
@@ -220,14 +238,12 @@ Map elements of the collection. Similar to `Array.map`, but `undefined` values r
220238

221239
Thus, `collection.map` can be used to map and filter elements in a single pass.
222240

223-
## Predicate methods
224-
225-
Predicate is either the iteratee function returning boolean, or an object with attribute values used to match with record's attributes.
226-
227241
### collection.filter( iteratee : Predicate, context? )
228242

229243
Return filtered array of records matching the predicate.
230244

245+
Predicate is either the iteratee function returning boolean, or an object with attribute values used to match with record's attributes.
246+
231247
### collection.every( iteratee : Predicate, context? ) : boolean
232248

233249
Return `true` if all records match the predicate.
@@ -242,6 +258,8 @@ Note that Type-R depends on the arity of your comparator function to determine b
242258

243259
Collections with a comparator will not automatically re-sort if you later change record attributes, so you may wish to call sort after changing record attributes that would affect the order.
244260

261+
## Sorting
262+
245263
### `static` comparator = 'attrName'
246264

247265
Maintain the collection in sorted order by the given record's attribute.
@@ -279,26 +297,7 @@ alert(chapters.map( x => x.title ));
279297

280298
Force a collection to re-sort itself. You don't need to call this under normal circumstances, as a collection with a comparator will sort itself whenever a record is added. To disable sorting when adding a record, pass `{sort: false}` to add. Calling sort triggers a "sort" event on the collection.
281299

282-
### collection.push( record, options? )
300+
## Serialization
283301

284-
Add a record at the end of a collection. Takes the same options as add.
285-
286-
### collection.pop( options? )
287-
Remove and return the last record from a collection. Takes the same options as remove.
288-
289-
### collection.unshift( record, options? )
290-
291-
Add a record at the beginning of a collection. Takes the same options as add.
292-
293-
### collection.shift( options? )
294-
Remove and return the first record from a collection. Takes the same options as remove.
295-
296-
### collection.slice( begin, end )
297-
298-
Return a shallow copy of the `collection.models`, using the same options as native Array#slice.
302+
## Validation
299303

300-
### collection.indexOf( recordOrId : any ) : number
301-
302-
Return an index of the record in the collection, and -1 if there are no such a record in the collection.
303-
304-
Can take the record itself as an argument, `id`, or `cid` of the record.

0 commit comments

Comments
 (0)