Collections Flashcards
You create a collection how?
var Songs = Backbone.Collection.extend()
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
What’s the third way you can add a model to a collectionk
collection.add(new Model({attributes}) .add is an underscore function
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
How to return the count of models inside the collection?
songs.length
where do a lot of the methods for collections come from?
underscore
true/false - You can you insert a model into a collection at a certain index?
true
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});
true/false - You can also use .push underscore method to add models to a collection?
true
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
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.
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’ }
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; });