You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/chapters/Events.md
+38-46Lines changed: 38 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,40 +1,38 @@
1
1
# Events
2
2
3
-
Both `Record` and `Collection` uses an efficient synchronous events implementation which is compatible with Backbone 1.1 Events API but is twice faster in average. It comes in form of `Events` mixin and the `Messenger` base class.
4
-
5
-
An implementation is optimized for the large amount of relatively small subscriptions (5-10 events). Here are the benchmark results (lower is the better).
6
-
7
-

8
-
9
-
It is also available separately as part of [MixtureJS](https://github.com/Volicon/MixtureJS) package.
3
+
Type-R uses an efficient synchronous events implementation which is backward compatible with Backbone 1.1 Events API but is about twice faster in all major browsers. It comes in form of `Events` mixin and the `Messenger` base class.
10
4
11
5
## Events mixin
12
6
13
-
`Events` is a [mixin](11_Mixins.md) giving the object the ability to bind and trigger custom named events. Events do not have to be declared before they are bound, and may take passed arguments.
7
+
`Events` is a [mixin](#mixins) giving the object the ability to bind and trigger custom named events. Events do not have to be declared before they are bound, and may take passed arguments.
8
+
9
+
Both `source` and `listener` mentioned in method signatures must implement Events methods.
14
10
15
11
```javascript
16
12
import { mixins, Events } from'type-r'
17
13
18
14
@mixins( Events )
19
-
classMessenger {
15
+
classEventfulClass {
20
16
...
21
17
}
22
18
```
23
19
24
-
> `Messenger` abstract base class is included with Type-R, see below.
20
+
<asideclass="notice">There's the <code>Messenger</code> abstract base class with Events mixed in.</aside>
25
21
26
-
### trigger(event, arg1, arg2, ... )
22
+
### source.trigger(event, arg1, arg2, ... )
27
23
28
24
Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.
29
25
30
-
### listenTo(source, event, callback)
26
+
### listener.listenTo(source, event, callback)
31
27
Tell an object to listen to a particular event on an other object. The advantage of using this form, instead of other.on(event, callback, object), is that listenTo allows the object to keep track of the events, and they can be removed all at once later on. The callback will always be called with object as context.
32
28
33
29
```javascript
34
30
view.listenTo(record, 'change', view.render );
35
31
```
36
32
37
-
### stopListening([source], [event], [callback])
33
+
<asideclass="success">Subscriptions made with <code>listenTo()</code> will be stopped automatically if an object is properly disposed (<code>dispose()</code> method is called).</aside>
Tell an object to stop listening to events. Either call stopListening with no arguments to have the object remove all of its registered callbacks ... or be more precise by telling it to remove just the events it's listening to on a specific object, or a specific event, or just a specific callback.
40
38
@@ -44,13 +42,13 @@ Tell an object to stop listening to events. Either call stopListening with no ar
44
42
view.stopListening(record); // Unsubscribe from all events from the record
45
43
```
46
44
47
-
All Type-R classes execute `this.stopListening()` from their `dispose()` method.
45
+
<asideclass="notice">Messenger, Record, Collection, and Store execute <code>this.stopListening()</code> from their <code>dispose()</code> method. You don't have to unsubscribe from events explicitly if you are using <code>listenTo()</code> method and disposing your objects properly.</aside>
Just like listenTo, but causes the bound callback to fire only once before being removed.
49
+
Just like `listenTo()`, but causes the bound callback to fire only once before being automatically removed.
52
50
53
-
### on(event, callback, [context])
51
+
### source.on(event, callback, [context])
54
52
55
53
Bind a callback function to an object. The callback will be invoked whenever the event is fired. If you have a large number of different events on a page, the convention is to use colons to namespace them: `poll:start`, or `change:selection`. The event string may also be a space-delimited list of several events...
56
54
@@ -78,7 +76,9 @@ All event methods also support an event map syntax, as an alternative to positio
78
76
79
77
To supply a context value for this when the callback is invoked, pass the optional last argument: `record.on('change', this.render, this)` or `record.on({change: this.render}, this)`.
80
78
81
-
### off([event], [callback], [context])
79
+
<asideclass="warning">Event subscription with <code>source.on()</code> may create memory leaks if it's not stopped properly with <code>source.off()</code></aside>
80
+
81
+
### source.off([event], [callback], [context])
82
82
83
83
Remove a previously bound callback function from an object. If no context is specified, all of the versions of the callback with different contexts will be removed. If no callback is specified, all callbacks for the event will be removed. If no event is specified, callbacks for all events will be removed.
84
84
@@ -101,7 +101,7 @@ Remove a previously bound callback function from an object. If no context is spe
101
101
102
102
Note that calling `record.off()`, for example, will indeed remove all events on the record — including events that Backbone uses for internal bookkeeping.
103
103
104
-
### once(event, callback, [context])
104
+
### source.once(event, callback, [context])
105
105
Just like `on()`, but causes the bound callback to fire only once before being removed. Handy for saying "the next time that X happens, do this". When multiple events are passed in using the space separated syntax, the event will fire once for every event you passed in, not once for a combination of all events
106
106
107
107
## Messenger class
@@ -120,51 +120,43 @@ class MyMessenger extends Messenger {
Copy file name to clipboardExpand all lines: docs/chapters/collection.md
+69-46Lines changed: 69 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,12 +73,7 @@ Initialization function which is called at the end of the constructor.
73
73
74
74
### collection.createSubset()
75
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).
76
+
## Read and iterate
82
77
83
78
### collection.get( id )
84
79
Get a record from a collection, specified by an `id`, a `cid`, or by passing in a record.
@@ -98,6 +93,59 @@ Like an array, a Collection maintains a length property, counting the number of
98
93
99
94
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.
100
95
96
+
### collection.slice( begin, end )
97
+
98
+
Return a shallow copy of the `collection.models`, using the same options as native Array#slice.
99
+
100
+
### collection.indexOf( recordOrId : any ) : number
101
+
102
+
Return an index of the record in the collection, and -1 if there are no such a record in the collection.
103
+
104
+
Can take the record itself as an argument, `id`, or `cid` of the record.
105
+
106
+
### collection.forEach( iteratee : ( val : Record, index ) => void, context? )
107
+
108
+
Same as `collection.each()`.
109
+
110
+
### collection.each( iteratee : ( val : Record, index ) => void, context? )
111
+
112
+
Iterate through the elements of the collection. Similar to `Array.forEach`.
113
+
114
+
<asideclass="notice">Use <code>collection.updateEach( iteratee, index )</code> method to update records in a loop.</aside>
115
+
116
+
### collection.map( iteratee : ( val : Record, index ) => T, context? )
117
+
118
+
Map elements of the collection. Similar to `Array.map`, but `undefined` values returned by iteratee are filtered out.
119
+
120
+
Thus, `collection.map` can be used to map and filter elements in a single pass.
Return `true` if at least one record match the predicated.
135
+
136
+
By default there is no comparator for a collection. If you define a comparator, it will be used to maintain the collection in sorted order. This means that as records are added, they are inserted at the correct index in `collection.models`.
137
+
138
+
Note that Type-R depends on the arity of your comparator function to determine between the two styles, so be careful if your comparator function is bound.
139
+
140
+
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.
141
+
142
+
## Update
143
+
144
+
Methods to update the collection. They accept common options:
145
+
146
+
-`sort : false` - do not sort the collection.
147
+
-`parse : true` - parse raw JSON (used to set collection with a data from the server).
148
+
101
149
### collection.add( records, options? )
102
150
103
151
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.
@@ -165,7 +213,7 @@ Any additional changes made to the collection or its items in event handlers wil
165
213
166
214
### collection.updateEach( iteratee : ( val : Record, index ) => void, context? )
167
215
168
-
Similar to the `collection.each`, but wraps the loop in a transaction.
216
+
Similar to the `collection.each`, but wraps an iteration in a transaction. The single `changes` event will be emitted for the group of changes to the records made in `updateEach`.
169
217
170
218
### collection.push( record, options? )
171
219
@@ -181,16 +229,6 @@ Add a record at the beginning of a collection. Takes the same options as add.
181
229
### collection.shift( options? )
182
230
Remove and return the first record from a collection. Takes the same options as remove.
183
231
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
232
## Change events
195
233
196
234
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.
@@ -215,48 +253,33 @@ Subscribe for events from records. The `hander` is either the collection's metho
215
253
216
254
When `true` is passed as a handler, the corresponding event will be triggered on the collection.
217
255
218
-
##Iteration
256
+
### `event` "changes" (collection, options)
219
257
220
-
###collection.forEach( iteratee : ( val : Record, index ) => void, context? )
258
+
When collection has changed. Single event triggered when the collection has been changed.
221
259
222
-
Same as `collection.each()`.
260
+
### `event` "reset" (collection, options)
223
261
224
-
### collection.each( iteratee : ( val : Record, index ) => void, context? )
262
+
When the collection's entire contents have been reset (`reset()` method was called).
225
263
226
-
Iterate through the elements of the collection. Similar to `Array.forEach`.
227
-
228
-
### collection.updateEach( iteratee : ( val : Record, index ) => void, context? )
229
-
230
-
Similar to the `collection.each`, but wraps an iteration in a transaction. The single `changes` event will be emitted
231
-
for the group of changes to the records made in `updateEach`.
232
-
233
-
*Use this method if you modify records in a loop*.
234
-
235
-
### collection.map( iteratee : ( val : Record, index ) => T, context? )
236
-
237
-
Map elements of the collection. Similar to `Array.map`, but `undefined` values returned by iteratee are filtered out.
238
-
239
-
Thus, `collection.map` can be used to map and filter elements in a single pass.
Return `true` if at least one record match the predicated.
278
+
When a record is removed from a collection.
254
279
255
-
By default there is no comparator for a collection. If you define a comparator, it will be used to maintain the collection in sorted order. This means that as records are added, they are inserted at the correct index in `collection.models`.
280
+
### `event` "change" (record, options)
256
281
257
-
Note that Type-R depends on the arity of your comparator function to determine between the two styles, so be careful if your comparator function is bound.
258
-
259
-
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.
282
+
When a record inside of the collection is changed.
0 commit comments