Next, we'll create users. Unlike some of our previous applications, these aren't user accounts that users can log into. Instead, a user is just someone who is currently using our chat application; all they need to provide is a screen name. So a collection of users is really just a list of currently used screen names.
So, create a public/user.js
file and start with the following code:
App.module('User', function (User) { var UserModel = Backbone.Model.extend({}); User.Collection = Backbone.Collection.extend({ model: UserModel, initialize: function () { var thiz = this; App.Socket.io.on('user:join', function (user) { thiz.add(user); }); App.Socket.io.on('user:leave', function (user) { thiz.findWhere(user).destroy(); }); } }); });
Here's how we start. First, we create a basic UserModel
class (we can't just call it User
because that would overwrite our module variable). Then, we create a collection class. As we've...