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

View File

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