Collections Flashcards Preview

Backbone > Collections > Flashcards

Flashcards in Collections Deck (13):
1

You create a collection how?

var Songs = Backbone.Collection.extend()

2

How do you tell the collection what model you use?

extend({
model: Song
}) or

you can pass directly when making the collection with new Song

3

What's the third way you can add a model to a collectionk

collection.add(new Model({attributes}) .add is an underscore function

4

How do you get a model from a collection?

you can do this with 2 ways. songs.at(0) where 0 is the index or with songs.get('c1') where c1 is the id or cid

5

How to return the count of models inside the collection?

songs.length

6

where do a lot of the methods for collections come from?

underscore

7

true/false - You can you insert a model into a collection at a certain index?

true

8

How do you insert a model at a certain index in a collection?

You pass it as the second argument to add.
songs.add(new Song({ title: 'Hello' }), {at: 0});

9

true/false - You can also use .push underscore method to add models to a collection?

true

10

What method can you use to find all matching models in a collection?

songs.where({ genre: 'Jazz' }); will return all matching models, and it returns an array

11

What method can you use to find the first occurrence of a model in a collection?

songs.findWhere({ genre: 'Jazz' }); will return the first instance found.

12

true/false You Can match multiple searches in a songs.where with backbone?

true - pass in multiple attributes to search { title: 'Song 2', genre: 'Jazz' }

13

How would you find all the models that have downloads greater than 2?

You can use the filter method.
var topDownloads = songs.filter(function(song) {
return song.get('downloads') > 100;
});