Skip to content

Commit 94087c9

Browse files
author
Vlad Balin
committed
Improved Events documentation.
1 parent 3c89d74 commit 94087c9

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

docsource/07_Events.md

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,37 @@ An implementation is optimized for the large amount of relatively small subscrip
88

99
Events is a mixin, 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.
1010

11+
#### eventsSource.trigger(event, [*args])
12+
13+
Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.
14+
15+
### High-level listening API
16+
17+
All high-level event subscriptions are stopped automatically on the listener's disposal, and thus does not introduce memory leaks.
18+
19+
This is the preferable listening API and must be used in all application code.
20+
21+
#### listener.listenTo(other, event, callback)
22+
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.
23+
24+
view.listenTo(model, 'change', view.render);
25+
26+
#### listener.stopListening([other], [event], [callback])
27+
28+
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.
29+
30+
view.stopListening();
31+
32+
view.stopListening(model);
33+
34+
#### listener.listenToOnce(other, event, callback)
35+
36+
Just like listenTo, but causes the bound callback to fire only once before being removed.
37+
38+
### Low-level listening API
39+
40+
This API is more efficient but requires manual action to stop the subscribtion. Must be used with care.
41+
1142
#### eventSource.on(event, callback, [context])
1243

1344
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...
@@ -51,30 +82,9 @@ Remove a previously-bound callback function from an object. If no context is spe
5182

5283
Note that calling `model.off()`, for example, will indeed remove all events on the model — including events that Backbone uses for internal bookkeeping.
5384

54-
#### eventsSource.trigger(event, [*args])
55-
56-
Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.
57-
5885
#### eventsSource.once(event, callback, [context])
5986
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
6087

61-
#### eventsSource.listenTo(other, event, callback)
62-
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.
63-
64-
view.listenTo(model, 'change', view.render);
65-
66-
#### eventsSource.stopListening([other], [event], [callback])
67-
68-
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.
69-
70-
view.stopListening();
71-
72-
view.stopListening(model);
73-
74-
#### eventsSource.listenToOnce(other, event, callback)
75-
76-
Just like listenTo, but causes the bound callback to fire only once before being removed.
77-
7888
## Messenger
7989

8090
Messenger is an abstract base class implementing Events mixin and some convinience methods.

0 commit comments

Comments
 (0)