Backbone.js: Events Cheat Sheet

MethodsSyntaxDescription
onobject.on(event, callback, [context])Bind a callback function to an object. The callback will be invoked whenever the event is fired.
offobject.off([event], [callback], [context])Remove a previously-bound callback function from an object. 
triggerobject.trigger(event, [*args])Trigger callbacks for the given event, or space-delimited list of events.
onceobject.once(event, callback, [context])It extends the backbone.Model class while creating your own backbone Model.
listenToobject.listenTo(other, event, callback)Tell an object to listen to a particular event on an other object. 
stopListeningobject.stopListening([other], [event], [callback])Tell an object to stop listening to events.
listenToOnceobject.listenToOnce(other, event, callback)It causes the listenTo occur only once before the callback function is being removed.

Catalog of Events

  • “add” (model, collection, options) — when a model is added to a collection.
  • “remove” (model, collection, options) — when a model is removed from a collection.
  • “update” (collection, options) — single event triggered after any number of models have been added, removed or changed in a collection.
  • “reset” (collection, options) — when the collection’s entire contents have been reset.
  • “sort” (collection, options) — when the collection has been re-sorted.
  • “change” (model, options) — when a model’s attributes have changed.
  • “change:[attribute]” (model, value, options) — when a specific attribute has been updated.
  • “destroy” (model, collection, options) — when a model is destroyed.
  • “request” (model_or_collection, xhr, options) — when a model or collection has started a request to the server.
  • “sync” (model_or_collection, response, options) — when a model or collection has been successfully synced with the server.
  • “error” (model_or_collection, xhr, options) — when a model’s or collection’s request to the server has failed.
  • “invalid” (model, error, options) — when a model’s validation fails on the client.
  • “route:[name]” (params) — Fired by the router when a specific route is matched.
  • “route” (route, params) — Fired by the router when any route has been matched.
  • “route” (router, route, params) — Fired by history when any route has been matched.
  • “all” — this special event fires for any triggered event, passing the event name as the first argument followed by all trigger arguments.

Leave a Reply

Your email address will not be published. Required fields are marked *