Permalink
Browse files

Separate out helpers

  • Loading branch information...
1 parent 6a13fcd commit 801fc67e9004906093f192c87a8a77e75faa34b1 @mjackson committed Apr 16, 2012
Showing with 41 additions and 43 deletions.
  1. +38 −0 public/helpers.js
  2. +3 −43 public/index.html
View
@@ -0,0 +1,38 @@
+// The handle of the chat user.
+var user = window.prompt("What username would you like to use?");
+
+// The address of the chat server.
+var host = "codeclass.herokuapp.com";
+
+// Sends the given `message` (a string) to everyone in the chat.
+function sendMessage(message) {
+ $.ajax("http://" + host + "/messages-create", {
+ dataType: "jsonp",
+ data: {
+ user: user,
+ message: message
+ }
+ });
+}
+
+// Fetches all new messages from the server and call the callback with
+// each new message.
+function getMessages(callback) {
+ $.ajax("http://" + host + "/messages", {
+ dataType: "jsonp",
+ data: {
+ since: since
+ },
+ success: function (data, textStatus, xhr) {
+ callback(data.messages);
+ }
+ });
+}
+
+// Shows the given `message` on the page.
+function renderMessage(message) {
+ var user = message.user;
+ var text = message.text;
+ var html = '<li class="message"><span class="user">' + user + ':</span><span class="text">' + text + '</span></li>';
+ $("#messages").append(html);
+}
View
@@ -6,52 +6,12 @@
<link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="/style.css">
<script type="text/javascript" src="/jquery.js"></script>
+ <script type="text/javascript" src="/helpers.js"></script>
<script type="text/javascript">
- // The handle of the chat user.
- var user = window.prompt("What username would you like to use?");
-
// The time (in milliseconds) of the last message we received.
var since = 0;
- // The address of the chat server.
- var host = "codeclass.herokuapp.com";
-
- // host = "localhost:5000";
-
- // Sends the given `message` (a string) to everyone in the chat.
- function sendMessage(message) {
- $.ajax("http://" + host + "/messages-create", {
- dataType: "jsonp",
- data: {
- user: user,
- message: message
- }
- });
- }
-
- // Fetches all new messages from the server and call the callback with
- // each new message.
- function getMessages(callback) {
- $.ajax("http://" + host + "/messages", {
- dataType: "jsonp",
- data: {
- since: since
- },
- success: function (data, textStatus, xhr) {
- callback(data.messages);
- }
- });
- }
-
- // Shows the given `message` on the page.
- function renderMessage(message) {
- var user = message.user;
- var text = message.text;
- var html = '<li class="message"><span class="user">' + user + ':</span><span class="text">' + text + '</span></li>';
- $("#messages").append(html);
- }
-
$(function () {
// Listen for "Enter" on the #new-message textarea. When it's pressed,
@@ -72,9 +32,9 @@
renderMessage(message);
});
- // Update the value of the `since` variable so we don't get the same
- // messages back again in the future.
if (messages.length > 0) {
+ // Update the value of the `since` variable so we don't get
+ // the same messages back again in the future.
since = messages[messages.length - 1].time;
}
});

0 comments on commit 801fc67

Please sign in to comment.