Reimplemented join/parts

This commit is contained in:
Mattias Erming 2014-03-29 16:59:28 -07:00
parent 2c8040a6ca
commit e3bfc43a78
3 changed files with 55 additions and 17 deletions

View File

@ -17,7 +17,6 @@ $(function() {
function event(type, json) { function event(type, json) {
console.log(json); console.log(json);
console.log(type);
switch (type) { switch (type) {

View File

@ -58,10 +58,10 @@ models.Channel = Backbone.Model.extend({
name: "", name: "",
}, },
addUser: function(models) { addUser: function(models) {
this.get("users").add(models); return this.get("users").add(models);
}, },
addMessage: function(models) { addMessage: function(models) {
this.get("messages").add(models); return this.get("messages").add(models);
}, },
initialize: function() { initialize: function() {
this.set({ this.set({
@ -79,11 +79,12 @@ models.Channel = Backbone.Model.extend({
}); });
}, this); }, this);
this.get("users").on("all", function(action, data) { var users = this.get("users");
users.on("all", function(action, data) {
this.trigger("user", { this.trigger("user", {
target: this.get("id"), target: this.get("id"),
type: "user", type: "user",
data: data, data: users,
action: action, action: action,
}); });
}, this); }, this);
@ -96,10 +97,11 @@ models.Channels = Backbone.Collection.extend({
models.Network = Backbone.Model.extend({ models.Network = Backbone.Model.extend({
defaults: { defaults: {
host: "" host: "",
client: null,
}, },
addChannel: function(models) { addChannel: function(models) {
this.get("channels").add(models); return this.get("channels").add(models);
}, },
initialize: function() { initialize: function() {
this.set({ this.set({
@ -124,6 +126,9 @@ models.Network = Backbone.Model.extend({
type: "network", type: "network",
name: this.get("host") name: this.get("host")
}); });
},
toJSON: function() {
return _.omit(this.attributes, "client");
} }
}); });

View File

@ -40,9 +40,6 @@ module.exports = function listen() {
}; };
function connect(host) { function connect(host) {
var network = networks.add({host: host}, {silent: true});
networks.trigger("network", networks);
var stream = net.connect({ var stream = net.connect({
port: 6667, port: 6667,
host: host, host: host,
@ -53,6 +50,12 @@ function connect(host) {
}); });
var client = irc(stream); var client = irc(stream);
var network = networks.add({
host: host,
client: client,
}, {silent: true});
networks.trigger("network", networks);
client.nick("t_user"); client.nick("t_user");
client.user("t_user", "temp user"); client.user("t_user", "temp user");
@ -104,13 +107,24 @@ function input(json) {
networks.remove(network); networks.remove(network);
break; break;
case "join":
if (args[1]) {
network.get("client").join(args[1]);
}
break;
case "part":
if (args[1]) {
network.get("client").part(args[1]);
}
break;
} }
} }
function event(event, data) { function event(event, data) {
var network = this; var channels = this.get("channels");
var channels = network.get("channels");
switch (event) { switch (event) {
case "away": case "away":
@ -118,7 +132,12 @@ function event(event, data) {
break; break;
case "join": case "join":
console.log(event); var chan = channels.findWhere({name: data.channel}) || channels.add({name: data.channel});
chan.addUser({name: data.nick});
chan.addMessage({
user: data.nick,
type: "join",
});
break; break;
case "kick": case "kick":
@ -126,7 +145,8 @@ function event(event, data) {
break; break;
case "names": case "names":
console.log(event); var chan = channels.findWhere({name: data.channel});
chan.get("users").reset(_.map(data.names, function(n) { return {name: n}; }));
break; break;
case "nick": case "nick":
@ -138,7 +158,21 @@ function event(event, data) {
break; break;
case "part": case "part":
console.log(event); var len = data.channels.length;
var name = data.nick;
for (var i = 0; i < len; i++) {
var chan = channels.findWhere({name: data.channels[i]});
if (name == this.get("client").me) {
channels.remove(chan);
return;
}
var users = channel.get("users");
users.remove(users.findWhere({name: name}));
chan.addMessage({
user: data.nick,
type: "part",
});
}
break; break;
case "pong": case "pong":
@ -158,7 +192,7 @@ function event(event, data) {
break; break;
case "welcome": case "welcome":
console.log(event); channels.first().addMessage({message: "Connected."});
break; break;
case "whois": case "whois":