Using the Backbone events

This commit is contained in:
Mattias Erming 2014-03-10 00:14:22 +01:00
parent ea2d20e118
commit 9ae50727ec
2 changed files with 79 additions and 39 deletions

View File

@ -39,10 +39,28 @@ models.Channel = Backbone.Model.extend({
}, },
initialize: function() { initialize: function() {
this.set({ this.set({
id: id++, id: id++
users: new models.UserCollection(),
messages: new models.MessageCollection()
}); });
this.set("users", new models.UserCollection());
this.get("users").on(
"all",
function() {
// Bubble event
this.trigger("change", this);
},
this
);
this.set("messages", new models.MessageCollection());
this.get("messages").on(
"all",
function() {
// Bubble event
this.trigger("change", this);
},
this
);
} }
}); });
@ -54,25 +72,41 @@ models.Network = Backbone.Model.extend({
defaults: { defaults: {
host: "", host: "",
nick: "default_username", nick: "default_username",
connect: false connect: true
}, },
initialize: function() { initialize: function() {
this.set({ this.set({
id: id++, id: id++,
channels: new models.ChannelCollection()
}); });
if (this.get("connect")) {
this.conn = new irc.Client( this.set("channels", new models.ChannelCollection());
this.get("host"), this.get("channels").on(
this.get("nick"), { "all",
channels: ["#testchan"] function() {
} // Bubble event
); this.trigger("change", this);
} },
this
);
this.get("channels").add(new models.Channel({ this.get("channels").add(new models.Channel({
type: "network", type: "network",
name: this.get("host") name: this.get("host")
})); }));
if (this.get("connect")) {
this.irc = new irc.Client(
this.get("host"),
this.get("nick"), {
channels: ["#test_channel"]
}
);
}
this.on("remove", function() {
if (typeof this.irc !== "undefined") {
this.irc.disconnect();
}
});
} }
}); });
@ -80,7 +114,8 @@ models.NetworkCollection = Backbone.Collection.extend({
model: models.Network, model: models.Network,
initialize: function() { initialize: function() {
this.add(new models.Network({ this.add(new models.Network({
host: "Lobby" host: "Lobby",
connect: false
})); }));
}, },
find: function(id) { find: function(id) {

View File

@ -19,6 +19,12 @@ Server.prototype.listen = function(port) {
this.sockets = io.listen(http).sockets; this.sockets = io.listen(http).sockets;
this.sockets.on("connection", function(socket) { this.sockets.on("connection", function(socket) {
self.networks.on(
"all",
function() {
self.sockets.emit("event", self.networks);
}
);
socket.emit( socket.emit(
"event", "event",
self.networks self.networks
@ -26,7 +32,7 @@ Server.prototype.listen = function(port) {
socket.on( socket.on(
"input", "input",
function(input) { function(input) {
handleUserInput.call(self, input); handleInput.call(self, input);
} }
); );
}); });
@ -34,7 +40,7 @@ Server.prototype.listen = function(port) {
return this; return this;
}; };
function handleUserInput(input) { function handleInput(input) {
var target = this.networks.find(input.id); var target = this.networks.find(input.id);
if (!target) { if (!target) {
return; return;
@ -45,7 +51,6 @@ function handleUserInput(input) {
: ""; : "";
switch (cmd) { switch (cmd) {
case "": case "":
target.channel.get("messages").add( target.channel.get("messages").add(
new models.Message({user: "user", text: input.text}) new models.Message({user: "user", text: input.text})
@ -53,26 +58,22 @@ function handleUserInput(input) {
break; break;
case "CONNECT": case "CONNECT":
var network = new models.Network({ if (!argv[1]) {
host: "irc.freenode.org", return;
connect: true }
}); var network = this.networks.add(new models.Network({
this.networks.add(network); host: argv[1]
var messages = network.get("channels").at(0).get("messages"); }));
messages.add( network.irc.addListener(
new models.Message({text: "Connecting..."}) "raw",
function() {
handleEvent.apply(network, arguments);
}
); );
break;
var self = this; case "DISCONNECT":
network.conn.addListener("raw", function(argv) { this.networks.remove(target.network);
messages.add(
new models.Message({user: argv.args[0], text: argv.args[1]})
);
self.sockets.emit(
"event",
self.networks
);
});
break; break;
default: default:
@ -80,11 +81,15 @@ function handleUserInput(input) {
new models.Message({text: "Command `/" + cmd + "` does not exist."}) new models.Message({text: "Command `/" + cmd + "` does not exist."})
); );
break; break;
} }
}
this.sockets.emit( function handleEvent(argv) {
"event", var network = this;
this.networks network.get("channels").at(0).get("messages").add(
new models.Message({
user: argv.args[0],
text: argv.args[1]
})
); );
} }