Permalink
Browse files

Send commands as JSON not forms.

  • Loading branch information...
1 parent 9488e39 commit 00553b60a3364bfa851e2f78506d5f390d270d52 @paul-lshift paul-lshift committed Jan 11, 2013
View
@@ -18,6 +18,6 @@
; Copied in rather than added as dependency:
-; node-uuid
-; https://github.com/broofa/node-uuid/blob/master/uuid.js
-; Version: https://github.com/broofa/node-uuid/commit/0cdff1fb0bfb063a09254152958c4334d39c34bd
+; https://github.com/broofa/node-uuid/blob/0cdff1fb0bfb063a09254152958c4334d39c34bd/uuid.js
+; https://github.com/douglascrockford/JSON-js/blob/43d7836c8ec9b31a02a31ae0c400bdae04d3650d/json2.js
+; https://github.com/macek/jquery-serialize-object/blob/300795024639a86b488db60ec0984330b3a7b523/jquery.serialize-object.js
@@ -5,7 +5,9 @@
<title>Proof-of-concept chat client</title>
<link rel="stylesheet" href="/css/style.css" type="text/css">
<!-- http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/ -->
+ <script src="js/json2.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
+ <script src="js/jquery.serialize-object.js" type="text/javascript"></script>
<script src="js/uuid.js" type="text/javascript"></script>
<script src="js/app.js" type="text/javascript"></script>
</head>
View
@@ -3,7 +3,8 @@ function sendCommand(type, body) {
var request = {
type: 'PUT'
,url: "/ajax/command/" + type + "/" + uuid.v4()
- ,data: body
+ ,contentType: "application/json"
+ ,data: JSON.stringify(body)
};
console.log("Sending command");
// Test idempotency - send everything twice
@@ -13,6 +14,13 @@ function sendCommand(type, body) {
$.ajax(request);
});
}
+function submitSendsCommand(form, type) {
+ form.submit(function(event) {
+ sendCommand(type, $(this).serializeObject());
+ this.reset();
+ return false;
+ })
+}
var handleEvent = {
newthread: function(event) {
$("#threads-here").append(
@@ -27,10 +35,7 @@ var handleEvent = {
"</form>" +
"</div>");
$("#thread-" + event.id + " h2").text(event.body.title);
- $("#thread-" + event.id + " form").submit(function(event) {
- sendCommand("subscribe", $(this).serialize());
- return false;
- });
+ submitSendsCommand($("#thread-" + event.id + " form"), "subscribe");
}
,subscribe: function(event) {
$("#thread-" + event.body.thread + " form").remove();
@@ -42,11 +47,7 @@ var handleEvent = {
"<input type=\"text\" name=\"message\" size=\"100\">" +
"<input type=\"submit\" value=\"Submit\">" +
"</form>");
- $("#thread-" + event.body.thread + " form").submit(function(event) {
- sendCommand("message", $(this).serialize());
- this.reset();
- return false;
- });
+ submitSendsCommand($("#thread-" + event.body.thread + " form"), "message");
}
,message: function(event) {
var li = document.createElement('li');
@@ -102,11 +103,7 @@ function readEvents(position) {
}
$(document).ready(function() {
console.log("Document ready");
- $("#newthread").submit(function(event) {
- sendCommand("newthread", $(this).serialize());
- this.reset();
- return false;
- });
+ submitSendsCommand($("#newthread"), "newthread");
$("#initfocus").focus();
readEvents("0");
});
@@ -0,0 +1,47 @@
+(function($) {
+ return $.fn.serializeObject = function() {
+ var json, patterns, push_counters,
+ _this = this;
+ json = {};
+ push_counters = {};
+ patterns = {
+ validate: /^[a-zA-Z][a-zA-Z0-9_]*(?:\[(?:\d*|[a-zA-Z0-9_]+)\])*$/,
+ key: /[a-zA-Z0-9_]+|(?=\[\])/g,
+ push: /^$/,
+ fixed: /^\d+$/,
+ named: /^[a-zA-Z0-9_]+$/
+ };
+ this.build = function(base, key, value) {
+ base[key] = value;
+ return base;
+ };
+ this.push_counter = function(key) {
+ if (push_counters[key] === void 0) {
+ push_counters[key] = 0;
+ }
+ return push_counters[key]++;
+ };
+ $.each($(this).serializeArray(), function(i, elem) {
+ var k, keys, merge, re, reverse_key;
+ if (!patterns.validate.test(elem.name)) {
+ return;
+ }
+ keys = elem.name.match(patterns.key);
+ merge = elem.value;
+ reverse_key = elem.name;
+ while ((k = keys.pop()) !== void 0) {
+ if (patterns.push.test(k)) {
+ re = new RegExp("\\[" + k + "\\]$");
+ reverse_key = reverse_key.replace(re, '');
+ merge = _this.build([], _this.push_counter(reverse_key), merge);
+ } else if (patterns.fixed.test(k)) {
+ merge = _this.build([], k, merge);
+ } else if (patterns.named.test(k)) {
+ merge = _this.build({}, k, merge);
+ }
+ }
+ return json = $.extend(true, json, merge);
+ });
+ return json;
+ };
+})(jQuery);
Oops, something went wrong.

0 comments on commit 00553b6

Please sign in to comment.