Moving back to mustache templating

This commit is contained in:
Mattias Erming 2014-04-01 20:09:08 +02:00
parent 6e03377d7d
commit c3aee3d025
2 changed files with 40 additions and 41 deletions

View File

@ -57,12 +57,7 @@
<script type="text/html" id="network"> <script type="text/html" id="network">
{{#networks}} {{#networks}}
<div id="network-{{id}}" class="network list-group"> <div id="network-{{id}}" class="network list-group">
{{#each channels}} {{> channels}}
<a href="{{name}}" id="channel-{{id}}" class="channel list-group-item">
<span class="badge pull-right"></span>
{{name}}
</a>
{{/each}}
</div> </div>
{{/networks}} {{/networks}}
</script> </script>
@ -85,23 +80,15 @@
<a href="#" class="right">Users</a> <a href="#" class="right">Users</a>
</div> </div>
<h1>{{name}}</h1> <h1>{{name}}</h1>
<button class="close btn btn-danger btn-sm ">Leave</button> <button class="close btn btn-danger btn-sm ">
Leave
</button>
</div> </div>
<div class="users"> <div class="users">
{{#each users}} {{> users}}
<a href="{{name}}" class="user">
{{mode}}{{name}}
</a>
{{/each}}
</div> </div>
<div class="messages"> <div class="messages">
{{#each messages}} {{> messages}}
<div class="message {{type}}">
<span class="time">{{time}}</span>
<a href="{{from}}" class="user">{{mode}}{{from}}</a>
<span class="text">{{type}} {{{link message}}}</span>
</div>
{{/each}}
</div> </div>
<form onSubmit="return false;"> <form onSubmit="return false;">
<input type="text" class="input" data-target="{{id}}"/> <input type="text" class="input" data-target="{{id}}"/>
@ -124,16 +111,16 @@
<div class="message {{type}}"> <div class="message {{type}}">
<span class="time">{{time}}</span> <span class="time">{{time}}</span>
<a href="{{from}}" class="user">{{mode}}{{from}}</a> <a href="{{from}}" class="user">{{mode}}{{from}}</a>
<span class="text">{{type}} {{{link message}}}</span> <span class="text">{{type}} {{message}}</span>
</div> </div>
{{/messages}} {{/messages}}
</script> </script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.0/jquery.cookie.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.0/jquery.cookie.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0-alpha.2/handlebars.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.2/mustache.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/URI.js/1.11.2/URI.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/URI.js/1.11.2/URI.min.js"></script>
<script src="/socket.io/socket.io.js"></script> <script src="/socket.io/socket.io.js"></script>
<script src="/js/lib/jquery.scrollGlue.js"></script> <script src="/js/lib/jquery.scrollGlue.js"></script>
<script src="/js/lib/bootstrap.js"></script> <script src="/js/lib/bootstrap.js"></script>

View File

@ -10,9 +10,18 @@ $(function() {
var sidebar = $("#sidebar"); var sidebar = $("#sidebar");
var tpl = []; var tpl = [];
function render(id, json) { function render(id, json, partials) {
tpl[id] = tpl[id] || Handlebars.compile($(id).html()); tpl[id] = tpl[id] || $(id).html();
return tpl[id](json); 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 event(type, json) { function event(type, json) {
@ -20,13 +29,17 @@ $(function() {
case "network": case "network":
var html = ""; var html = "";
var partials = {
users: render("#user"),
messages: render("#message")
};
json.forEach(function(network) { json.forEach(function(network) {
html += render("#window", network); html += render("#window", network, partials);
}); });
$("#windows")[0].innerHTML = html; $("#windows")[0].innerHTML = html;
sidebar.find("#list").html( sidebar.find("#list").html(
render("#network", {networks: json}) render("#network", {networks: json}, {channels: render("#channel")})
).find(".channel") ).find(".channel")
.first() .first()
.addClass("active"); .addClass("active");
@ -38,8 +51,7 @@ $(function() {
break; break;
case "channel": case "channel":
var id = json.data.id; var id = json.data.id;
console.log(json.data);
if (json.action == "remove") { if (json.action == "remove") {
$("#channel-" + id + ", #window-" + id).remove(); $("#channel-" + id + ", #window-" + id).remove();
return; return;
@ -63,9 +75,9 @@ $(function() {
break; break;
case "user": case "user":
var target; var target = chat.find("#window-" + json.target);
if (typeof json.target !== "undefined") { if (target.size() == 0) {
target = chat.find("#window-" + json.target); return;
} }
target = target.find(".users"); target = target.find(".users");
@ -73,9 +85,9 @@ $(function() {
break; break;
case "message": case "message":
var target; var target = $("#window-" + json.target);
if (typeof json.target !== "undefined") { if (target.size() == 0) {
target = $("#window-" + json.target); return;
} }
var message = json.data; var message = json.data;
@ -188,9 +200,9 @@ $(function() {
}; };
}); });
Handlebars.registerHelper("link", function(text) { //Handlebars.registerHelper("link", function(text) {
var text = Handlebars.Utils.escapeExpression(text); // var text = Handlebars.Utils.escapeExpression(text);
return URI.withinString(text, function(url) { // return URI.withinString(text, function(url) {
return "<a href='" + url + "' target='_blank'>" + url + "</a>"; // return "<a href='" + url + "' target='_blank'>" + url + "</a>";
}); // });
}); //});