Using the Backbone events
This commit is contained in:
parent
ea2d20e118
commit
9ae50727ec
|
@ -39,10 +39,28 @@ models.Channel = Backbone.Model.extend({
|
|||
},
|
||||
initialize: function() {
|
||||
this.set({
|
||||
id: id++,
|
||||
users: new models.UserCollection(),
|
||||
messages: new models.MessageCollection()
|
||||
id: id++
|
||||
});
|
||||
|
||||
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: {
|
||||
host: "",
|
||||
nick: "default_username",
|
||||
connect: false
|
||||
connect: true
|
||||
},
|
||||
initialize: function() {
|
||||
this.set({
|
||||
id: id++,
|
||||
channels: new models.ChannelCollection()
|
||||
});
|
||||
if (this.get("connect")) {
|
||||
this.conn = new irc.Client(
|
||||
this.get("host"),
|
||||
this.get("nick"), {
|
||||
channels: ["#testchan"]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
this.set("channels", new models.ChannelCollection());
|
||||
this.get("channels").on(
|
||||
"all",
|
||||
function() {
|
||||
// Bubble event
|
||||
this.trigger("change", this);
|
||||
},
|
||||
this
|
||||
);
|
||||
this.get("channels").add(new models.Channel({
|
||||
type: "network",
|
||||
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,
|
||||
initialize: function() {
|
||||
this.add(new models.Network({
|
||||
host: "Lobby"
|
||||
host: "Lobby",
|
||||
connect: false
|
||||
}));
|
||||
},
|
||||
find: function(id) {
|
||||
|
|
|
@ -19,6 +19,12 @@ Server.prototype.listen = function(port) {
|
|||
|
||||
this.sockets = io.listen(http).sockets;
|
||||
this.sockets.on("connection", function(socket) {
|
||||
self.networks.on(
|
||||
"all",
|
||||
function() {
|
||||
self.sockets.emit("event", self.networks);
|
||||
}
|
||||
);
|
||||
socket.emit(
|
||||
"event",
|
||||
self.networks
|
||||
|
@ -26,7 +32,7 @@ Server.prototype.listen = function(port) {
|
|||
socket.on(
|
||||
"input",
|
||||
function(input) {
|
||||
handleUserInput.call(self, input);
|
||||
handleInput.call(self, input);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
@ -34,7 +40,7 @@ Server.prototype.listen = function(port) {
|
|||
return this;
|
||||
};
|
||||
|
||||
function handleUserInput(input) {
|
||||
function handleInput(input) {
|
||||
var target = this.networks.find(input.id);
|
||||
if (!target) {
|
||||
return;
|
||||
|
@ -45,7 +51,6 @@ function handleUserInput(input) {
|
|||
: "";
|
||||
|
||||
switch (cmd) {
|
||||
|
||||
case "":
|
||||
target.channel.get("messages").add(
|
||||
new models.Message({user: "user", text: input.text})
|
||||
|
@ -53,26 +58,22 @@ function handleUserInput(input) {
|
|||
break;
|
||||
|
||||
case "CONNECT":
|
||||
var network = new models.Network({
|
||||
host: "irc.freenode.org",
|
||||
connect: true
|
||||
});
|
||||
this.networks.add(network);
|
||||
var messages = network.get("channels").at(0).get("messages");
|
||||
messages.add(
|
||||
new models.Message({text: "Connecting..."})
|
||||
if (!argv[1]) {
|
||||
return;
|
||||
}
|
||||
var network = this.networks.add(new models.Network({
|
||||
host: argv[1]
|
||||
}));
|
||||
network.irc.addListener(
|
||||
"raw",
|
||||
function() {
|
||||
handleEvent.apply(network, arguments);
|
||||
}
|
||||
);
|
||||
break;
|
||||
|
||||
var self = this;
|
||||
network.conn.addListener("raw", function(argv) {
|
||||
messages.add(
|
||||
new models.Message({user: argv.args[0], text: argv.args[1]})
|
||||
);
|
||||
self.sockets.emit(
|
||||
"event",
|
||||
self.networks
|
||||
);
|
||||
});
|
||||
case "DISCONNECT":
|
||||
this.networks.remove(target.network);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -80,11 +81,15 @@ function handleUserInput(input) {
|
|||
new models.Message({text: "Command `/" + cmd + "` does not exist."})
|
||||
);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
this.sockets.emit(
|
||||
"event",
|
||||
this.networks
|
||||
function handleEvent(argv) {
|
||||
var network = this;
|
||||
network.get("channels").at(0).get("messages").add(
|
||||
new models.Message({
|
||||
user: argv.args[0],
|
||||
text: argv.args[1]
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue