Skip to content

Commit

Permalink
adding mustache
Browse files Browse the repository at this point in the history
  • Loading branch information
paras151 committed Aug 5, 2019
1 parent 1f917b6 commit 2284eb4
Show file tree
Hide file tree
Showing 3 changed files with 736 additions and 10 deletions.
28 changes: 28 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,37 @@ <h3>People</h3>

</div>

<script id="message-template" type="text">
<li class="message">
<div class="message__title">
<h4>{{from}}</h4>
<span>{{createdAt}}</span>

</div>
<div class="message__body">
<p>{{text}}</p>
</div>
</li>
</script>

<script id="location-message-template" type="text">
<li class="message">
<div class="message__title">
<h4>{{from}}</h4>
<span>{{createdAt}}</span>

</div>
<div class="message__body">
<a href="{{url}}" target="_blank">My current location</a>
</div>
</li>
</script>

<script src = "/socket.io/socket.io.js"></script>
<script src = "js/libs/jquery-3.4.1.min.js"></script>
<script src = "js/libs/moment.js"></script>
<script src = "js/libs/mustache.js"></script>
<script src = "js/index.js"></script>

</body>
</html>
36 changes: 26 additions & 10 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,36 @@ socket.on('disconnect',function(){
socket.on('newMessage',function(message){
// console.log("New Message",message);
var formattedTime = moment(message.createdAt).format('LT');
var li = jQuery('<li></li>');
li.text(`${message.from} ${formattedTime}: ${message.text}`)
jQuery('#messages').append(li);
// var li = jQuery('<li></li>');
// li.text(`${message.from} ${formattedTime}: ${message.text}`)
// jQuery('#messages').append(li);

var template = jQuery('#message-template').html();
var html = Mustache.render(template,{
text:message.text,
from:message.from,
createdAt:formattedTime
});

jQuery('#messages').append(html);
})

socket.on('newLocationMessage',function(message){
var li = jQuery('<li></li>');
var a = jQuery('<a target="_blank">My current location</a>')
// var li = jQuery('<li></li>');
// var a = jQuery('<a target="_blank">My current location</a>')
var formattedTime = moment(message.createdAt).format('LT');

li.text(`${message.from} ${formattedTime}: `);
a.attr('href',message.url);
li.append(a);
jQuery('#messages').append(li);
var template = jQuery('#location-message-template').html();
var html = Mustache.render(template,{
from:message.from,
url:message.url,
createdAt:formattedTime
});
jQuery('#messages').append(html);

// li.text(`${message.from} ${formattedTime}: `);
// a.attr('href',message.url);
// li.append(a);
// jQuery('#messages').append(li);
})


Expand Down
Loading

0 comments on commit 2284eb4

Please sign in to comment.