Shuo/lib/models.js

143 lines
2.6 KiB
JavaScript
Raw Normal View History

2014-03-09 21:22:37 +00:00
var irc = require("irc");
2014-03-07 21:24:02 +00:00
var Backbone = require("backbone");
2014-03-06 23:17:08 +00:00
var moment = require("moment");
2014-03-06 18:02:43 +00:00
2014-03-13 15:25:01 +00:00
// Local library
var config = require("./../config.js");
2014-03-07 21:24:02 +00:00
var models =
module.exports =
{};
2014-03-07 03:18:53 +00:00
var id = 1;
2014-03-06 15:11:25 +00:00
2014-03-07 21:24:02 +00:00
models.User = Backbone.Model.extend({
defaults: {
2014-03-13 15:25:01 +00:00
name: ""
2014-03-07 21:24:02 +00:00
}
});
2014-03-06 15:11:25 +00:00
2014-03-09 21:22:37 +00:00
models.UserCollection = Backbone.Collection.extend({
2014-03-12 15:09:37 +00:00
model: models.User,
comparator: function(user) {
return user.get("name");
}
2014-03-09 21:22:37 +00:00
});
2014-03-07 21:24:02 +00:00
models.Message = Backbone.Model.extend({
defaults: {
2014-03-12 15:09:37 +00:00
time: "",
2014-03-09 21:22:37 +00:00
user: "",
2014-03-13 15:50:33 +00:00
text: "",
type: "normal"
2014-03-12 15:09:37 +00:00
},
initialize: function() {
this.set("time", moment().format("HH:mm"));
2014-03-07 21:24:02 +00:00
}
});
2014-03-06 18:02:43 +00:00
2014-03-09 21:22:37 +00:00
models.MessageCollection = Backbone.Collection.extend({
model: models.Message
});
2014-03-07 21:24:02 +00:00
models.Channel = Backbone.Model.extend({
defaults: {
2014-03-06 22:36:56 +00:00
type: "channel",
2014-03-12 21:58:24 +00:00
name: ""
2014-03-07 21:24:02 +00:00
},
initialize: function() {
this.set({
2014-03-09 23:14:22 +00:00
id: id++
2014-03-07 21:24:02 +00:00
});
2014-03-09 23:14:22 +00:00
this.set("users", new models.UserCollection());
2014-03-13 12:44:54 +00:00
this.get("users").on("all", function() {
2014-03-14 21:57:54 +00:00
this.trigger("USERS", {
2014-03-13 12:44:54 +00:00
target: this.get("id"),
data: this.get("users")
});
}, this);
2014-03-09 23:14:22 +00:00
this.set("messages", new models.MessageCollection());
2014-03-13 12:44:54 +00:00
this.get("messages").on("all", function() {
2014-03-14 21:57:54 +00:00
this.trigger("MESSAGES", {
2014-03-13 12:44:54 +00:00
target: this.get("id"),
data: this.get("messages").last()
});
}, this);
2014-03-07 21:24:02 +00:00
}
});
2014-03-06 15:11:25 +00:00
2014-03-07 21:24:02 +00:00
models.ChannelCollection = Backbone.Collection.extend({
model: models.Channel
});
2014-03-06 15:11:25 +00:00
2014-03-07 21:24:02 +00:00
models.Network = Backbone.Model.extend({
defaults: {
host: "",
2014-03-13 17:50:02 +00:00
nick: config.nick
2014-03-07 21:24:02 +00:00
},
initialize: function() {
this.set({
2014-03-13 15:25:01 +00:00
id: id++
2014-03-07 21:24:02 +00:00
});
2014-03-09 23:14:22 +00:00
this.set("channels", new models.ChannelCollection());
2014-03-13 12:44:54 +00:00
this.get("channels").on("all", function(type, data) {
2014-03-14 21:57:54 +00:00
if (type == "USERS" || type == "MESSAGES") {
2014-03-13 12:44:54 +00:00
this.trigger(type, data);
} else {
2014-03-14 21:57:54 +00:00
this.trigger("CHANNELS");
2014-03-13 12:44:54 +00:00
}
}, this);
2014-03-09 23:14:22 +00:00
this.get("channels").add(new models.Channel({
type: "network",
name: this.get("host")
}));
2014-03-13 17:50:02 +00:00
},
connect: function(channels) {
var client = new irc.Client(
this.get("host"),
this.get("nick"), {
fullname: config.fullname,
channels: channels
}
);
2014-03-09 23:14:22 +00:00
2014-03-13 17:50:02 +00:00
this.irc = client;
this.irc.addListener("error", function() {
// ..
});
2014-03-09 23:14:22 +00:00
this.on("remove", function() {
if (typeof this.irc !== "undefined") {
this.irc.disconnect();
}
});
2014-03-13 17:50:02 +00:00
return this.irc;
2014-03-07 21:24:02 +00:00
}
});
2014-03-07 03:18:53 +00:00
2014-03-07 21:24:02 +00:00
models.NetworkCollection = Backbone.Collection.extend({
model: models.Network,
initialize: function() {
this.add(new models.Network({
2014-03-09 23:14:22 +00:00
host: "Lobby",
connect: false
2014-03-07 21:24:02 +00:00
}));
2014-03-09 19:27:44 +00:00
},
2014-03-09 21:22:37 +00:00
find: function(id) {
2014-03-09 19:27:44 +00:00
var networks = this.models;
for (var i = 0; i < networks.length; i++) {
var find = networks[i].get("channels").findWhere({id: id});
if (find) {
2014-03-09 21:22:37 +00:00
return {
network: networks[i],
channel: find
};
2014-03-09 19:27:44 +00:00
}
}
2014-03-07 21:24:02 +00:00
}
});