FREELessons: 24Length: 1.8 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.4 Using Collection Methods

Once you have a collection full of models, it's likely that you may occasionally want to get out a specific model, or work with the collection in some way. So, in this lesson, we're going to look at a couple of the different methods that collections give you. Before we begin, I just want to show you that I've created a new collection here, and I've added four books to the collection, just so we have something to work with. So when I load this page up in the browser, you can see that we have our collection here. If I say books.pluck, and I just grab the titles, you can see we have our four titles there. Now first let me show you that our collection has a models property, which is an array where the actual models are stored. You probably don't ever want to work with this array directly. You wanna use the proper collection methods to access it. But, you should know that this is where those collections are, actually, stored. First, we can use a collections get method to get a specific model by its ID. So, if I try and get the model with the ID three, you can see that we have a model returned. To see which model this is I can say get three, .get('title'), and you can see that we have "The Time Machine". We can also get a model by its position in the collection. So I can say, and if I want to get the model at index 3 you can see that this time we get "Sign of the Four". We can also use the where method and pass it a query object similar to what you would do with MongoDB. So if I want to find all the books where the author is Arthur C Doyle, then you can see what is returned is an array of two objects. And you can see we have two objects here. The first one is Sign of the Four and the second one is His Last Bow. Now that was the where method. However if we use the findWhere method, it will return only the first model that matches this query. So if we find where the author is Arthur Conan Doyle, you can see that the only book returned is Sign of the Four. Even though there are multiple books that would match this query, we only want the first one. So those are the few methods specific to the Backbone collections that you might use on collections. However, collections actually inherit quite a few methods from underscore. As you can see from the Backbone documentation here, they actually inherit 28 methods and these range from forEach Map to Reduce, Filter, Every, and Some, Min and Max, a bunch of other things here. And we can actually see these in practice. So for example I can say books.filter, and I can filter by returning if the, let's say, model.getauthor is equal to H G Wells. And obviously, there are simpler ways to do this, but just as an example, you can see that we only have one book that has an author of H G Wells. Of course this may be a bad example of using filter, but you can see that it's just as easy to use as a lot of those underscore and array methods are with just regular arrays. And so it's very rare that you would ever loop over a collection without using one of these methods. And I think you'll find that most of these underscore methods will be useful at some point in your Backbone applications. Just for one example, the forEach method here is very useful when you're rendering a collection inside of your view, you probably want to loop over each model in that collection, and render each model individually, but most of these underscore methods will be useful to you at one time or another. So you might wanna check these out. So that is how you can use some of the different methods that Backbone gives your collections.

Back to the top