Introduction to Backbone.js Part 4: Collections – Video Tutorial

Part 4: Backbone.js Collections Video Tutorial is here now. We’ve already discussed models, views, and routers in Backbone.js so far, but now we’re on to collections. Collections are pretty much exactly what their name implies: collections of models. Many times, data is displayed in lists, not just singular items. This is where collections come in - to keep every model in order and synchronized with the database on the back end.

Backbone.js Video Tutorial Series

Some JavaScript Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var Person = Backbone.Model.extend({
initialize: function() {
console.log('Person is initialized.');
},
defaults: {
name: 'undefined',
age: 'undefined'
}
});
var People = Backbone.Collection.extend({
initialize: function() {
console.log("People Collection is initialized");
},
model: Person
});
var person = new Person({name:"Joe"});
var people = new People(person);
people.add([{name:"Bob"}, {name:"Jim"}]);
console.log(people.toJSON());

Concluding Backbone.js Collections

That wraps up Collections. On Monday, April 16th, the 5th and “final” part of this series – AJAX – will be available. After that we’ll resume other posts for a week or two before moving on to creating video tutorials to create a full app using Backbone.js. Got any ideas you want to see built? Let me know in the comments below. Also, make sure to share this with all of your friends and followers so we can get their ideas for an app. Happy Coding!

Backbone.js Video Tutorial Series

Author: Joe Zimmerman

Author: Joe Zimmerman Joe Zimmerman has been doing web development ever since he found an HTML book on his dad's shelf when he was 12. Since then, JavaScript has grown in popularity and he has become passionate about it. He also loves to teach others though his blog and other popular blogs. When he's not writing code, he's spending time with his wife and children and leading them in God's Word.