From 9e860b14d71a685926b1e86773342d62d28337be Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 20 Apr 2014 23:48:05 +0200 Subject: [PATCH] Use handlebars instead of mustache --- client/index.html | 42 ++++++++++++++++++++++------------------ client/js/chat.js | 49 ++++++++++++++++------------------------------- lib/models.js | 2 +- 3 files changed, 41 insertions(+), 52 deletions(-) diff --git a/client/index.html b/client/index.html index 217d51d1..5bb815d9 100644 --- a/client/index.html +++ b/client/index.html @@ -53,23 +53,25 @@ - - - - - - + diff --git a/client/js/chat.js b/client/js/chat.js index bb278435..68c54fac 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -30,39 +30,31 @@ $(function() { }); var tpl = []; - function render(id, json, partials) { - tpl[id] = tpl[id] || $(id).html(); - if (!json) { - // If no data is supplied, return the template instead. - // Handy when fetching partials. - return tpl[id]; - } - return Mustache.render( - tpl[id], - json, - partials || {} - ); + function render(id, json) { + tpl[id] = tpl[id] || Handlebars.compile($(id).html()); + return tpl[id](json); } + Handlebars.registerHelper( + "partial", + function(id) { + return new Handlebars.SafeString(render(id, this)); + } + ); + function handleEvent(event, json) { var data = json.data; switch (event) { case "network": var html = ""; - var partials = { - users: render("#users"), - messages: render("#messages"), - }; data.forEach(function(n) { - html += render( - "#windows", {windows: n.channels}, partials - ); + html += render("#windows", {windows: n.channels}); }); chat[0].innerHTML = html; sidebar.find("#list").html( - render("#networks", {networks: data}, {channels: render("#channels")}) + render("#networks", {networks: data}) ).find(".channel") .first() .addClass("active") @@ -104,13 +96,13 @@ $(function() { sidebar.find(".channel").removeClass("active"); $("#network-" + json.target).append( - render("#channels", {channels: data}) + render("#channels", {channels: [data]}) ).find(".channel") .last() .addClass("active"); chat.append( - render("#windows", {windows: data}) + render("#windows", {windows: [data]}) ).find(".window") .last() .find("input") @@ -124,8 +116,8 @@ $(function() { break; case "user": - var target = chat.find("#window-" + json.target).find(".users"); - target.html(render("#users", {users: data})); + var html = render("#users", {users: data}); + var target = chat.find("#window-" + json.target + " .users").html(html); break; case "message": @@ -138,7 +130,7 @@ $(function() { target = target.parent().find(".active"); } - var msg = $(render("#messages", {messages: data})) + var msg = $(render("#messages", {messages: [data]})); target = target.find(".messages"); target.append(msg); @@ -253,13 +245,6 @@ $(function() { $(this).closest(".window").find(".messages").scrollToBottom(); }); - chat.on("mouseover", ".text", function() { - var self = $(this); - if (!self.hasClass("parsed")) { - self.addClass("parsed").html(uri(self.html())); - } - }); - function uri(text) { return URI.withinString(text, function(url) { return "" + url + ""; diff --git a/lib/models.js b/lib/models.js index b6bb874a..d65841f6 100644 --- a/lib/models.js +++ b/lib/models.js @@ -30,7 +30,7 @@ models.Message = Backbone.Model.extend({ defaults: { type: "", time: "", - from: "", + from: "-!-", message: "", }, initialize: function() {