From f86f67ff2459f8299201e3965aba561c986b3030 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Tue, 11 Mar 2014 23:57:29 +0100 Subject: [PATCH] Added custom events --- client/js/chat.js | 2 +- lib/models.js | 12 ++++++------ lib/server.js | 23 +++++++++++++++-------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/client/js/chat.js b/client/js/chat.js index d5ae48f..85829c6 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -101,7 +101,7 @@ $(function() { }; $.fn.scrollToBottom = function() { - this.scrollTop(this.prop("scrollHeight")); + this.scrollTop(1e10); }; $.fn.isScrollAtBottom = function() { diff --git a/lib/models.js b/lib/models.js index 2b300f5..43294f6 100644 --- a/lib/models.js +++ b/lib/models.js @@ -45,19 +45,19 @@ models.Channel = Backbone.Model.extend({ this.set("users", new models.UserCollection()); this.get("users").on( "all", - function() { + function(e) { // Bubble event - this.trigger("change", this); + this.trigger("user", this); }, this ); this.set("messages", new models.MessageCollection()); this.get("messages").on( - "all", + "add", function() { // Bubble event - this.trigger("change", this); + this.trigger("message", this); }, this ); @@ -82,9 +82,9 @@ models.Network = Backbone.Model.extend({ this.set("channels", new models.ChannelCollection()); this.get("channels").on( "all", - function() { + function(type) { // Bubble event - this.trigger("change", this); + this.trigger(type == "user" || type == "message" ? type : "channel", this); }, this ); diff --git a/lib/server.js b/lib/server.js index 77f2271..ddad9c7 100644 --- a/lib/server.js +++ b/lib/server.js @@ -16,15 +16,19 @@ Server.prototype.listen = function(port) { var http = connect() .use(connect.static("client")) .listen(port); - - this.sockets = io.listen(http).sockets; + + this.networks.on( + "all", + function(type) { + self.sockets.emit("event", self.networks); + + // Debug + console.log(type); + } + ); + + this.sockets = io.listen(http, {log: false}).sockets; this.sockets.on("connection", function(socket) { - self.networks.on( - "all", - function() { - self.sockets.emit("event", self.networks); - } - ); socket.emit( "event", self.networks @@ -92,4 +96,7 @@ function handleEvent(argv) { text: argv.args[1] }) ); + + // Debug + console.log(argv); }