Refactoring

This commit is contained in:
Mattias Erming 2014-03-24 17:47:14 +01:00
parent d88967e712
commit 15a23d131e
2 changed files with 45 additions and 42 deletions

View File

@ -1,5 +1,5 @@
var _ = require("lodash"); var _ = require("lodash");
var backbone = require("backbone"); var Backbone = require("backbone");
var moment = require("moment"); var moment = require("moment");
var id = 1; var id = 1;
@ -7,14 +7,14 @@ var models =
module.exports = module.exports =
{}; {};
models.User = backbone.Model.extend({ models.User = Backbone.Model.extend({
defaults: { defaults: {
mode: "", mode: "",
name: "", name: "",
} }
}); });
models.Users = backbone.Collection.extend({ models.Users = Backbone.Collection.extend({
model: models.User, model: models.User,
sort: function(options) { sort: function(options) {
this.models = _.sortBy( this.models = _.sortBy(
@ -37,7 +37,7 @@ models.Users = backbone.Collection.extend({
} }
}); });
models.Message = backbone.Model.extend({ models.Message = Backbone.Model.extend({
defaults: { defaults: {
type: "", type: "",
time: "", time: "",
@ -49,82 +49,86 @@ models.Message = backbone.Model.extend({
} }
}); });
models.Messages = backbone.Collection.extend({ models.Messages = Backbone.Collection.extend({
model: models.Message model: models.Message
}); });
models.Channel = backbone.Model.extend({ models.Channel = Backbone.Model.extend({
defaults: { defaults: {
type: "channel", type: "channel",
name: "" name: "",
},
addUser: function(models) {
this.get("users").add(models);
},
addMessage: function(models) {
this.get("messages").add(models);
}, },
initialize: function() { initialize: function() {
this.set({ this.set({
id: id++ id: id++,
messages: new models.Messages,
users: new models.Users,
}); });
this.set("messages", new models.Messages());
this.get("messages").on("all", function(action, data) { this.get("messages").on("all", function(action, data) {
this.trigger("message", { this.trigger("message", {
target: this.get("id"), target: this.get("id"),
type: "message", type: "message",
data: data, data: data,
action: action action: action,
}); });
}, this); }, this);
this.set("users", new models.Users()); this.get("users").on("all", function(action, data) {
this.get("users").on("all", function(action) {
this.trigger("user", { this.trigger("user", {
target: this.get("id"), target: this.get("id"),
type: "user", type: "user",
data: this.get("users"), data: data,
action: action action: action,
}); });
}, this); }, this);
} }
}); });
models.Channels = backbone.Collection.extend({ models.Channels = Backbone.Collection.extend({
model: models.Channel model: models.Channel
}); });
models.Network = backbone.Model.extend({ models.Network = Backbone.Model.extend({
defaults: { defaults: {
host: "" host: ""
}, },
addChannel: function(models) {
this.get("channels").add(models);
},
initialize: function() { initialize: function() {
this.set({ this.set({
id: id++ id: id++,
channels: new models.Channels,
}); });
this.set("channels", new models.Channels()); this.get("channels").on("message user", function() { this.trigger(action, data); }, this);
this.get("channels").on("all", function(action, data) { this.get("channels").on("all", function(action, data) {
if (action == "user" || action == "message") {
this.trigger(action, data);
} else {
this.trigger("channel", { this.trigger("channel", {
target: this.get("id"), target: this.get("id"),
type: "channel", type: "channel",
data: data, data: data,
action: action action: action,
}); });
}
}, this); }, this);
this.get("channels").add(new models.Channel({ this.addChannel({
type: "network", type: "network",
name: this.get("host") name: this.get("host")
})); });
} }
}); });
models.Networks = backbone.Collection.extend({ models.Networks = Backbone.Collection.extend({
model: models.Network, model: models.Network,
initialize: function() { initialize: function() {
this.add(new models.Network({ this.add({host: "Status"});
host: "Status"
}));
}, },
find: function(id) { find: function(id) {
var networks = this.models; var networks = this.models;

View File

@ -5,7 +5,7 @@ var models = require(__dirname + "/models");
var net = require("net"); var net = require("net");
var _ = require("lodash"); var _ = require("lodash");
var sockets; var sockets = null;
var networks = new models.Networks; var networks = new models.Networks;
var events = [ var events = [
@ -40,8 +40,7 @@ module.exports = function listen() {
}; };
function connect(host) { function connect(host) {
var network = new models.Network({host: host}, {silent: true}); var network = networks.add({host: host}, {silent: true});
networks.add(network);
networks.trigger("network", networks); networks.trigger("network", networks);
var stream = net.connect({ var stream = net.connect({
@ -88,7 +87,7 @@ function input(json) {
channel.get("name") channel.get("name")
); );
case "msg": case "msg":
channel.get("messages").add(new models.Message({text: _.tail(args, 2)})); channel.addMessage({message: _.tail(args, 2)});
break; break;
case "server": case "server":
@ -133,7 +132,7 @@ function event(event, data) {
break; break;
case "notice": case "notice":
channels.first().get("messages").add(new models.Message(data)); channels.first().addMessage(data);
break; break;
case "part": case "part":