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
+45-57Lines changed: 45 additions & 57 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,51 +23,18 @@ class Messenger {
23
23
24
24
> `Messenger` abstract base class is included with Type-R, see below.
25
25
26
-
### eventsSource.trigger(event, arg1, arg2, ... )
26
+
### trigger(event, arg1, arg2, ... )
27
27
28
28
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
29
30
-
## Messenger
31
-
32
-
Messenger is an abstract base class implementing Events mixin and some convenience methods.
33
-
As all Type-R classes, its definition must be preceded with the `@define` decorator.
34
-
35
-
```javascript
36
-
import { define, Messenger } from'type-r'
37
-
38
-
@define classMyMessengerextendsMessenger {
39
-
40
-
}
41
-
```
42
-
43
-
### `readonly` messenger.cid
44
-
45
-
Unique run-time only messenger instance id (string).
46
-
47
-
### messenger.initialize()
48
-
49
-
Callback which is called at the end of the constructor.
50
-
51
-
### messenger.dispose()
52
-
53
-
Executes `messenger.stopListening()` and `messenger.off()`.
54
-
55
-
Objects must be disposed to prevent memory leaks caused by subscribing for events from singletons.
56
-
57
-
## High-level listening API
58
-
59
-
All high-level event subscriptions are stopped automatically on the listener's disposal, and thus does not introduce memory leaks.
60
-
61
-
This is the preferable listening API and must be used in all application code.
62
-
63
-
### listener.listenTo(other, event, callback)
30
+
### listenTo(source, event, callback)
64
31
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.
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.
73
40
@@ -79,15 +46,11 @@ Tell an object to stop listening to events. Either call stopListening with no ar
79
46
80
47
All Type-R classes execute `this.stopListening()` from their `dispose()` method.
81
48
82
-
### listener.listenToOnce(other, event, callback)
49
+
### listenToOnce(source, event, callback)
83
50
84
51
Just like listenTo, but causes the bound callback to fire only once before being removed.
85
52
86
-
## Low-level listening API
87
-
88
-
This API is more efficient but requires manual action to stop the subscription. Must be used with care.
89
-
90
-
### eventSource.on(event, callback, [context])
53
+
### on(event, callback, [context])
91
54
92
55
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...
93
56
@@ -115,7 +78,7 @@ All event methods also support an event map syntax, as an alternative to positio
115
78
116
79
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)`.
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.
121
84
@@ -138,32 +101,60 @@ Remove a previously bound callback function from an object. If no context is spe
138
101
139
102
Note that calling `record.off()`, for example, will indeed remove all events on the record — including events that Backbone uses for internal bookkeeping.
140
103
141
-
### eventsSource.once(event, callback, [context])
104
+
### once(event, callback, [context])
142
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
0 commit comments