Collection Flashcards

1
Q

extend

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

model

A
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);
    }
  }

});

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

constructor / initialise

A

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'
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

models

A

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.

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

toJSON

A

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));

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

sync

A

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.

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

add

A

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.

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

remove

A

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.

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

reset

A

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:
);

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

set

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

get

A

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);

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

at

A

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.

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

push

A

push
collection.push(model, [options])

Add a model at the end of a collection. Takes the same options as add.

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

pop

A

pop
collection.pop([options])

Remove and return the last model from a collection. Takes the same options as remove.

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

unshift

A

unshift
collection.unshift(model, [options])

Add a model at the beginning of a collection. Takes the same options as add.

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

shift

A

shift
collection.shift([options])

Remove and return the first model from a collection. Takes the same options as remove.

17
Q

slice

A

slice
collection.slice(begin, end)

Return a shallow copy of this collection’s models, using the same options as native Array#slice.

18
Q

length

A

length
collection.length

Like an array, a Collection maintains a length property, counting the number of models it contains.

19
Q

comparator

A

comparator

collection.comparator

By default there is no comparator for a collection. If you define a comparator, it will be used to maintain the collection in sorted order. This means that as models are added, they are inserted at the correct index in collection.models. A comparator can be defined as a sortBy (pass a function that takes a single argument), as a sort (pass a comparator function that expects two arguments), or as a string indicating the attribute to sort by.

“sortBy” comparator functions take a model and return a numeric or string value by which the model should be ordered relative to others. “sort” comparator functions take two models, and return -1 if the first model should come before the second, 0 if they are of the same rank and 1 if the first model should come after.

Note how even though all of the chapters in this example are added backwards, they come out in the proper order:

var Chapter  = Backbone.Model;
var chapters = new Backbone.Collection;

chapters.comparator = function(chapter) {
return chapter.get(“page”);
};

chapters. add(new Chapter({page: 9, title: “The End”}));
chapters. add(new Chapter({page: 5, title: “The Middle”}));
chapters. add(new Chapter({page: 1, title: “The Beginning”}));

alert(chapters.pluck(‘title’));

Collections with a comparator will not automatically re-sort if you later change model attributes, so you may wish to call sort after changing model attributes that would affect the order.

20
Q

sort

A

sort

collection.sort([options])

Force a collection to re-sort itself. You don’t need to call this under normal circumstances, as a collection with a comparator will sort itself whenever a model is added. To disable sorting when adding a model, pass {sort: false} to add. Calling sort triggers a “sort” event on the collection.

21
Q

pluck

A

pluckcollection.pluck(attribute)
Pluck an attribute from each model in the collection. Equivalent to calling map and returning a single attribute from the iterator.

var stooges = new Backbone.Collection([
  {name: "Curly"},
  {name: "Larry"},
  {name: "Moe"}
]);

var names = stooges.pluck(“name”);

alert(JSON.stringify(names));

22
Q

where

A

where

collection.where(attributes)

Return an array of all the models in a collection that match the passed attributes. Useful for simple cases of filter.

var friends = new Backbone.Collection([
  {name: "Athos",      job: "Musketeer"},
  {name: "Porthos",    job: "Musketeer"},
  {name: "Aramis",     job: "Musketeer"},
  {name: "d'Artagnan", job: "Guard"},
]);

var musketeers = friends.where({job: “Musketeer”});

alert(musketeers.length);

23
Q

findwhere

A

findWhere

collection.findWhere(attributes)

Just like where, but directly returns only the first model in the collection that matches the passed attributes.

24
Q

url

A

url

collection.url or collection.url()

Set the url property (or function) on a collection to reference its location on the server. Models within the collection will use url to construct URLs of their own.

var Notes = Backbone.Collection.extend({
  url: '/notes'
});

// Or, something more sophisticated:

var Notes = Backbone.Collection.extend({
  url: function() {
    return this.document.url() + '/notes';
  }
});
25
Q

parse

A

parse

collection.parse(response, options)

parse is called by Backbone whenever a collection’s models are returned by the server, in fetch. The function is passed the raw response object, and should return the array of model attributes to be added to the collection. The default implementation is a no-op, simply passing through the JSON response. Override this if you need to work with a preexisting API, or better namespace your responses.

var Tweets = Backbone.Collection.extend({
  // The Twitter Search API returns tweets under "results".
  parse: function(response) {
    return response.results;
  }
});
26
Q

clone

A

clone

collection.clone()

Returns a new instance of the collection with an identical list of models.

27
Q

fetch

A

fetch

collection.fetch([options])

Fetch the default set of models for this collection from the server, setting them on the collection when they arrive. The options hash takes success and error callbacks which will both be passed (collection, response, options) as arguments. When the model data returns from the server, it uses set to (intelligently) merge the fetched models, unless you pass {reset: true}, in which case the collection will be (efficiently) reset. Delegates to Backbone.sync under the covers for custom persistence strategies and returns a jqXHR. The server handler for fetch requests should return a JSON array of models.

Backbone.sync = function(method, model) {
alert(method + “: “ + model.url);
};

var accounts = new Backbone.Collection;
accounts.url = '/accounts';

accounts.fetch();

The behavior of fetch can be customized by using the available set options. For example, to fetch a collection, getting an “add” event for every new model, and a “change” event for every changed existing model, without removing anything: collection.fetch({remove: false})

jQuery.ajax options can also be passed directly as fetch options, so to fetch a specific page of a paginated collection: documents.fetch({data: {page: 3}})

Note that fetch should not be used to populate collections on page load — all models needed at load time should already be bootstrapped in to place. fetch is intended for lazily-loading models for interfaces that are not needed immediately: for example, documents with collections of notes that may be toggled open and closed.

28
Q

create

A

1