Collection Flashcards
extend
extend Backbone.Collection.extend(properties, [classProperties]) To create a Collection class of your own, extend Backbone.Collection, providing instance properties, as well as optional classProperties to be attached directly to the collection's constructor function.
model
model collection.model Override this property to specify the model class that the collection contains. If defined, you can pass raw attributes objects (and arrays) to add, create, and reset, and the attributes will be converted into a model of the proper type. var Library = Backbone.Collection.extend({ model: Book });
A collection can also contain polymorphic models by overriding this property with a function that returns a model. var Library = Backbone.Collection.extend({
model: function(attrs, options) { if (condition) { return new PublicDocument(attrs, options); } else { return new PrivateDocument(attrs, options); } }
});
constructor / initialise
constructor / initializenew
Collection([models], [options])
When creating a Collection, you may choose to pass in the initial array of models. The collection’s comparator may be included as an option. Passing false as the comparator option will prevent sorting. If you define an initialize function, it will be invoked when the collection is created. There are several options that, if provided, are attached to the collection directly: url, model and comparator.
var tabs = new TabSet([tab1, tab2, tab3]); var spaces = new Backbone.Collection([], { model: Space, url: '/spaces' });
models
models
collection.models
Raw access to the JavaScript array of models inside of the collection. Usually you’ll want to use get, at, or the Underscore methods to access model objects, but occasionally a direct reference to the array is desired.
toJSON
toJSON
collection.toJSON()
Return an array containing the attributes hash of each model in the collection. This can be used to serialize and persist the collection as a whole. The name of this method is a bit confusing, because it conforms to JavaScript’s JSON API.
var collection = new Backbone.Collection([ {name: "Tim", age: 5}, {name: "Ida", age: 26}, {name: "Rob", age: 55} ]);
alert(JSON.stringify(collection));
sync
sync
collection.sync(method, collection, [options])
Uses Backbone.sync to persist the state of a collection to the server. Can be overridden for custom behavior.
add
add
collection.add(models, [options])
Add a model (or an array of models) to the collection, firing an “add” event. If a model property is defined, you may also pass raw attributes objects, and have them be vivified as instances of the model. Pass {at: index} to splice the model into the collection at the specified index. If you’re adding models to the collection that are already in the collection, they’ll be ignored, unless you pass {merge: true}, in which case their attributes will be merged into the corresponding models, firing any appropriate “change” events.
var ships = new Backbone.Collection;
ships.on(“add”, function(ship) {
alert(“Ahoy “ + ship.get(“name”) + “!”);
});
ships.add([
{name: “Flying Dutchman”},
{name: “Black Pearl”}
]);
Note that adding the same model (a model with the same id) to a collection more than once is a no-op.
remove
remove
collection.remove(models, [options])
Remove a model (or an array of models) from the collection. Fires a “remove” event, which you can use silent to suppress. The model’s index before removal is available to listeners as options.index.
reset
reset
collection.reset([models], [options])
Adding and removing models one at a time is all well and good, but sometimes you have so many models to change that you’d rather just update the collection in bulk. Use reset to replace a collection with a new list of models (or attribute hashes), triggering a single “reset” event at the end. For convenience, within a “reset” event, the list of any previous models is available as options.previousModels.
Here’s an example using reset to bootstrap a collection during initial page load, in a Rails application:
);
set
set
collection.set(models, [options])
The set method performs a “smart” update of the collection with the passed list of models. If a model in the list isn’t yet in the collection it will be added; if the model is already in the collection its attributes will be merged; and if the collection contains any models that aren’t present in the list, they’ll be removed. All of the appropriate “add”, “remove”, and “change” events are fired as this happens. If you’d like to customize the behavior, you can disable it with options: {add: false}, {remove: false}, or {merge: false}.
var vanHalen = new Collection([eddie, alex, stone, roth]);
vanHalen.set([eddie, alex, stone, hagar]);
// Fires a "remove" event for roth, and an "add" event for "hagar". // Updates any of stone, alex, and eddie's attributes that may have // changed over the years.
get
get
collection.get(id)
Get a model from a collection, specified by an id, a cid, or by passing in a model.
var book = library.get(110);
at
at
collection.at(index)
Get a model from a collection, specified by index. Useful if your collection is sorted, and if your collection isn’t sorted, at will still retrieve models in insertion order.
push
push
collection.push(model, [options])
Add a model at the end of a collection. Takes the same options as add.
pop
pop
collection.pop([options])
Remove and return the last model from a collection. Takes the same options as remove.
unshift
unshift
collection.unshift(model, [options])
Add a model at the beginning of a collection. Takes the same options as add.