changeset 53:67ec969d4f0d

If the sending of a message fails, the content is put back into the text field.
author Atul Varma <varmaa@toolness.com>
date Tue, 28 Apr 2009 14:12:46 -0700
parents 3071f85fb694
children d1331955845f
files openwebchat.js
diffstat 1 files changed, 16 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/openwebchat.js	Tue Apr 28 14:00:00 2009 -0700
+++ b/openwebchat.js	Tue Apr 28 14:12:46 2009 -0700
@@ -30,11 +30,18 @@
     req.send(null);
   },
 
-  sendMessage: function sendMessage(msg) {
+  sendMessage: function sendMessage(options) {
     var req = new XMLHttpRequest();
     req.open('POST', 'send', true);
     req.overrideMimeType('application/json');
-    req.send(JSON.stringify(msg));
+    req.addEventListener(
+      "error",
+      function() {
+        options.onError(null);
+      },
+      false
+    );
+    req.send(JSON.stringify(options.message));
   }
 };
 
@@ -69,6 +76,7 @@
 
     $('#outgoing-message').keydown(
       function(evt) {
+        var self = this;
         var content = $(this).val();
         var author = $('#name').val();
         if (evt.keyCode == ENTER_KEYCODE) {
@@ -79,7 +87,12 @@
                        time: new Date()};
             if (author)
               msg.author = author;
-            OpenWebChat.sendMessage(msg);
+            OpenWebChat.sendMessage(
+              {message: msg,
+               onError: function(exception) {
+                 $(self).val(content);
+               }
+              });
           }
           evt.preventDefault();
         }