Collections Flashcards

1
Q

You create a collection how?

A

var Songs = Backbone.Collection.extend()

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

How do you tell the collection what model you use?

A

extend({
model: Song
}) or

you can pass directly when making the collection with new Song

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

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

A

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

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

How do you get a model from a collection?

A

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

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

How to return the count of models inside the collection?

A

songs.length

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

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

A

underscore

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

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

A

true

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

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

A
You pass it as the second argument to add.
songs.add(new Song({ title: 'Hello' }), {at: 0});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

true

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

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

A

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

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

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

A

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

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

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

A

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

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

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

A
You can use the filter method. 
var topDownloads = songs.filter(function(song) {
    return song.get('downloads') > 100;
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly