Events Flashcards

1
Q

on

A

on

object.on(event, callback, [context]) Alias: bind

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…

book.on(“change:title change:author”, …);

To supply a context value for this when the callback is invoked, pass the optional third argument: model.on(‘change’, this.render, this)

Callbacks bound to the special “all” event will be triggered when any event occurs, and are passed the name of the event as the first argument. For example, to proxy all events from one object to another:

proxy.on(“all”, function(eventName) {
object.trigger(eventName);
});

All Backbone event methods also support an event map syntax, as an alternative to positional arguments:

book.on({
  "change:title": titleView.update,
  "change:author": authorPane.update,
  "destroy": bookView.remove
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

off

A

off

object.off([event], [callback], [context]) Alias: unbind

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.

// Removes just the `onChange` callback.
object.off("change", onChange);
// Removes all "change" callbacks.
object.off("change");
// Removes the `onChange` callback for all events.
object.off(null, onChange);
// Removes all callbacks for `context` for all events.
object.off(null, null, context);
// Removes all callbacks on `object`.
object.off();

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

trigger

A

trigger

object.trigger(event, [*args])

Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

once

A

once

object.once(event, callback, [context])

Just like on, but causes the bound callback to only fire once before being removed. Handy for saying “the next time that X happens, do this”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

listenTo

A

listenTo

object.listenTo(other, event, callback)

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), is that listenTo allows the object to keep track of the events, and they can be removed all at once later on.

view.listenTo(model, ‘change’, view.render);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

stopListening

A

stopListening

object.stopListening([other], [event], [callback])

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.

view. stopListening();
view. stopListening(model);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

listenOnce

A

listenToOnce

object.listenToOnce(other, event, callback)

Just like listenTo, but causes the bound callback to only fire once before being removed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly