- Overview
- Transcript
3.1 Creating Collections
Collections are the containers that Backbone has for holding models. We'll look at how to create them in this lesson.
1.Getting Started3 lessons, 13:56
1.1Introduction01:04
1.2Backbone Data Flow03:16
1.3Backbone Classes09:36
2.Backbone Models7 lessons, 38:03
2.1Creating Models03:19
2.2Working With Model Attributes07:44
2.3Validating Models04:04
2.4Persisting Models to the Server08:35
2.5Using Model IDs03:43
2.6Listening for Model Events07:47
2.7Creating Model Methods02:51
3.Backbone Collections6 lessons, 17:52
3.1Creating Collections01:10
3.2Managing Collection Contents03:14
3.3Syncing Collections04:18
3.4Using Collection Methods03:14
3.5Sorting Collections02:42
3.6Listening for Collection Events03:14
4.Backbone Views4 lessons, 26:08
4.1Creating Views03:18
4.2Rendering Views07:25
4.3Managing DOM Events10:42
4.4Persisting Data With Views04:43
5.Backbone Routers3 lessons, 11:19
5.1Creating Routers01:59
5.2Adding Routes04:26
5.3Navigating With Links04:54
6.Conclusion1 lesson, 01:36
6.1Conclusion01:36
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.