Backbone.js Flashcards
What is Backbone.js?
Backbone.js gives structure to web applications by providing:
- models with key-value binding and custom events,
- collections with a rich API of enumerable functions,
- views with declarative event handling, and
- connects it all to your existing API over a RESTful JSON interface.
Events
LL OOO ST
listenTo
listenToOnce
once
on
off
stopListening
trigger
bind a callback function to an object that’s invoked whenever the event is fired
object. on(event, callback)
book. on(“change:title change:author”, doSomething);
remove previously bound callback function from an object
object.off([event], [callback], [context])
// 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();
Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.
object.trigger(event, [*args])
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”.
object.once(event, callback, [context])
Tell an object to listen to a particular event on an other object.
Advantage?
object.listenTo(other, event, callback)
Advantage: listenTo allows the object to keep track of the events, and they can be removed all at once later on.
Tell an object to stop listening to events.
object.stopListening([other], [event], [callback])
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.
just like listenTo, but causes the bound callback to only fire once before being removed.
object.listenToOnce(other, event, callback)
Built-in Backbone events - master list
AA CCDEI 6R 2S
add, all
change, change: [attribute]
destroy, error, invalid
remove, reset, request, route, route (router, route, params), route: [name]
sort, sync
Backbone events - AA
“add” (model, collection, options) — when a model is added to a collection.
“all” — this special event fires for any triggered event, passing the event name as the first argument.
Backbone events - CCDEI
“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.
“error” (model, xhr, options) — when a model’s save call fails on the server.
“invalid” (model, error, options) — when a model’s validation fails on the client.
Backbone events - 6R
“remove” (model, collection, options) — when a model is removed from a collection.
“reset” (collection, options) — when the collection’s entire contents have been replaced.
“request” (model, xhr, options) — when a model (or collection) has started a request to the server.
“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.
Backbone events - 2S
“sort” (collection, options) — when the collection has been re-sorted.
“sync” (model, resp, options) — when a model (or collection) has been successfully synced with the server.
jQuery events - master list
CCD 3F H2K L 7M(DELMOOU) 2R 2S U