FREELessons: 24Length: 1.8 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.1 Creating Collections

Now that we've covered models, it's time to move on to collections, which are sets of models. A collection just makes it easier to work with multiple models. You can loop over the collection to say, render a view for each model. Or you can filter through the collection to find models with specific attributes. Underneath our book model here, let's create our first collection. Usually a collection will be named the plural form of whatever its model is. So in this case, since our model is book, we'll call our collection books. We can make a collection by doing Backbone.Collection.extend. Now I should make it clear that you don't have to create a model class to go with your collection class. Collection classes can work just as well on their own, however you'll often want to associate your Backbone model with your Backbone collection. And the way you do this is with the model property. So, we'll set the model equal to Book so that when we create a new Books collection, it will know that each of the models it contains are models from the Book class. Of course after this, it's very easy to create a collection. We can just say new books and there you go. We have our new collection. So in the next couple of lessons, we'll look at using Backbone Collections.

Back to the top