Search code examples
javascriptbackbone.js

What are the default Backbone events?


I'm being a little lazy here but are these (below) all the default Backbone events. Also and am I correct in saying that the events bubble so a collection will receive any events triggered by a model.

Backbone Model events
change
error
sync
destroy

Collection events
add
remove
sync
reset

Many thanks
Lazy Nick


Solution

  • From the Backbone website ( http://backbonejs.org/#Events-catalog ):

    Catalog of Events

    Here's a list of all of the built-in events that Backbone.js can fire. You're also free to trigger your own events on Models and Views as you see fit.

    • "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 or removed from a collection.
    • "reset" (collection, options) — when the collection's entire contents have been replaced.
    • "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, resp, options) — when a model or collection has been successfully synced with the server.
    • "error" (model_or_collection, resp, 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.

    As for event bubbling, generally yes. When a model event is triggered it usually bubbles up through the collection as well. I'm not 100% sure if this is the case 100% of the time, but it usually is at least for the built in events.