summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-05-19 21:19:31 +0300
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-05-19 21:20:39 +0300
commite6ed34c6228492e798f23fa8ee9aacb8840ac54d (patch)
tree10a400c099e44b361e5191aecbe16ff6bc52e963
parent798bd31d507d13d27e92d576a09dafb96cb49453 (diff)
downloadaconf-e6ed34c6228492e798f23fa8ee9aacb8840ac54d.tar.bz2
aconf-e6ed34c6228492e798f23fa8ee9aacb8840ac54d.tar.xz
web client: refactor HTTP request helper functions
-rw-r--r--web/client.js66
1 files changed, 35 insertions, 31 deletions
diff --git a/web/client.js b/web/client.js
index c788984..50840d3 100644
--- a/web/client.js
+++ b/web/client.js
@@ -1,4 +1,4 @@
-/*
+/*
* Copyright (c) 2012-2013 Kaarle Ritvanen
* See LICENSE file for license details
*/
@@ -10,26 +10,31 @@ $(function() {
}).done(function(data, status, xhr) {
var token = xhr.getResponseHeader("X-ACF-Auth-Token");
+ var txn;
- function request(url, txn, options) {
+ function request(url, options) {
options = options || {};
options.headers = {"X-ACF-Auth-Token": token};
if (txn) options.headers["X-ACF-Transaction-ID"] = txn;
return $.ajax(url, options);
}
- var txn, changed, invalid;
+ function objRequest(path, options) {
+ return request("/config" + path, options);
+ }
+
+ var changed, invalid;
function startTxn() {
var def = $.Deferred();
- request("/", null, {type: "POST"})
- .done(function(data, status, xhr) {
- txn = xhr.getResponseHeader("X-ACF-Transaction-ID");
- changed = {};
- invalid = {};
- $("#status").empty();
- def.resolve();
- });
+ txn = null;
+ request("/", {type: "POST"}).done(function(data, status, xhr) {
+ txn = xhr.getResponseHeader("X-ACF-Transaction-ID");
+ changed = {};
+ invalid = {};
+ $("#status").empty();
+ def.resolve();
+ });
return def;
}
@@ -77,7 +82,7 @@ $(function() {
init: function(value, meta) { this.el = this.format(value); }
}
- widgets = {
+ var widgets = {
boolean: {
format: function(value) { return value ? "Yes" : "No"; },
init: function(value, meta) {
@@ -116,9 +121,8 @@ $(function() {
function render() {
var path = $.param.fragment();
- var url = "/config" + path;
- request(url, txn).done(function(data) {
+ objRequest(path).done(function(data) {
var content = $("#content");
content.html($("<h1>").text(path));
@@ -131,14 +135,14 @@ $(function() {
var table = $("<table>");
- function renderField(url, name, value, meta, editable) {
+ function renderField(path, name, value, meta, editable) {
var row = $("<tr>");
if (name != null) row.append($("<td>").text(name));
var td = $("<td>");
var msg = $("<div>");
- if (url in changed) msg.text("[changed]");
+ if (path in changed) msg.text("[changed]");
td.html(msg);
if (meta.widget in widgets) {
@@ -148,7 +152,7 @@ $(function() {
if (editable) {
var widget = Object.create(widget);
widget.init(
- url in invalid ? invalid[url] : value, meta
+ path in invalid ? invalid[path] : value, meta
);
el = widget.el;
el.change(function() {
@@ -175,12 +179,12 @@ $(function() {
}
}
}
- var task = request(url, txn, options);
+ var task = objRequest(path, options);
- if (!(url in changed)) changed[url] = value;
- if (newValue == changed[url])
- delete changed[url];
- invalid[url] = newValue;
+ if (!(path in changed)) changed[path] = value;
+ if (newValue == changed[path])
+ delete changed[path];
+ invalid[path] = newValue;
msg.text("[checking]");
@@ -196,7 +200,7 @@ $(function() {
}
function abortTxn() {
- request("/", txn, {type: "DELETE"});
+ request("/", {type: "DELETE"});
return newTxn();
}
@@ -210,7 +214,7 @@ $(function() {
value: "Commit",
disabled: true
}).click(function() {
- request("/", txn, {type: "PUT"})
+ request("/", {type: "PUT"})
.done(newTxn)
.fail(function(xhr) {
abortTxn().done(function() {
@@ -227,10 +231,10 @@ $(function() {
}).click(abortTxn));
task.done(function() {
- if (url in changed) msg.text("[changed]");
+ if (path in changed) msg.text("[changed]");
else msg.empty();
- delete invalid[url];
+ delete invalid[path];
if (!(_.size(invalid)))
$("#commit").prop("disabled", false);
@@ -239,7 +243,7 @@ $(function() {
});
});
- if (url in invalid) el.trigger("change");
+ if (path in invalid) el.trigger("change");
}
else el = widget.format(value);
@@ -263,7 +267,7 @@ $(function() {
}
renderField(
- url + "/" + name,
+ path + "/" + name,
field["ui-name"],
data.data[field.name],
field,
@@ -274,7 +278,7 @@ $(function() {
else _.each(data.data, function(value) {
var i = 1;
renderField(
- url + "/" + i++,
+ path + "/" + i++,
null,
value,
data.meta.members,
@@ -290,5 +294,5 @@ $(function() {
$(window).bind("hashchange", render);
$.bbq.pushState("#/");
});
- })
-})
+ });
+});