summaryrefslogtreecommitdiffstats
path: root/web
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-05-21 15:52:10 +0300
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-05-21 16:27:00 +0300
commit0f66997b445d49799dd5a24f89f5e4be7dbdfc82 (patch)
treee8566d9fdcc432c1bf2a69bc9090b290987dba97 /web
parentb6e2950cd4c9e58764e97ac2085c6fb5be65bef4 (diff)
downloadaconf-0f66997b445d49799dd5a24f89f5e4be7dbdfc82.tar.bz2
aconf-0f66997b445d49799dd5a24f89f5e4be7dbdfc82.tar.xz
web client: fix a couple of corner cases in validation
wait until previous request completes for the same value do not issue DELETE request for non-existent values
Diffstat (limited to 'web')
-rw-r--r--web/client.js145
1 files changed, 89 insertions, 56 deletions
diff --git a/web/client.js b/web/client.js
index 42e5e6f..898d37d 100644
--- a/web/client.js
+++ b/web/client.js
@@ -34,7 +34,7 @@ $(function() {
txn = xhr.getResponseHeader("X-ACF-Transaction-ID");
changed = {};
invalid = {};
- $("#status").prop("class", "");
+ $("#status").prop("class", null);
statusBar.empty();
buttons.prop("class", "hidden");
def.resolve();
@@ -257,69 +257,102 @@ $(function() {
if (editable) {
widget = Object.create(widget);
widget.init(
- path in invalid ? invalid[path] : value, meta
+ path in invalid ? invalid[path][0] : value,
+ meta
);
el = widget.el;
+
el.change(function() {
- var options;
- var newValue = widget.get();
- if (newValue == null)
- options = {type: "DELETE"}
- else options = {
- type: "PUT",
- data: JSON.stringify(newValue)
- }
- var task = objRequest(path, options);
-
- if (!(path in changed)) changed[path] = value;
- if (newValue == changed[path])
- delete changed[path];
- invalid[path] = newValue;
-
- msg.text("[checking]");
- if ($("#status").prop("class") != "invalid")
- statusBar.text("Validating changes");
-
- function setStatus(status, msg, commit) {
- $("#status").prop("class", status);
- statusBar.text(msg);
- $("#commit").prop("disabled", !commit);
- buttons.prop("class", "");
+
+ function ensureTask(task) {
+ if (task) return task;
+ task = $.Deferred();
+ task.resolve();
+ return task;
}
- task.done(function() {
- msg.empty();
- widget.setStatus(
- path in changed ? "changed" : null
- );
+ var prevTask;
+ if (path in invalid)
+ prevTask = invalid[path][1];
+ prevTask = ensureTask(prevTask);
+
+ prevTask.always(function() {
+ var options;
+ var newValue = widget.get();
+ if (newValue != null)
+ options = {
+ type: "PUT",
+ data: JSON.stringify(newValue)
+ };
+ else {
+ var savedValue = (path in changed) ?
+ changed[path][1] : value;
+ if (savedValue != null)
+ options = {type: "DELETE"};
+ }
+
+ var task;
+ if (options)
+ task = objRequest(path, options);
+ task = ensureTask(task);
+
+ invalid[path] = [newValue, task];
+
+ msg.text("[checking]");
+ if (
+ $("#status").prop("class") != "invalid"
+ ) statusBar.text("Validating changes");
+
+
+ function setStatus(status, msg, commit) {
+ $("#status").prop("class", status);
+ statusBar.text(msg);
+ $("#commit").prop("disabled", !commit);
+ buttons.prop("class", null);
+ }
+
+ task.done(function() {
+ if (!(path in changed))
+ changed[path] = [value];
+ if (newValue == changed[path][0])
+ delete changed[path];
+ else changed[path][1] = newValue;
- delete invalid[path];
- if (!(_.size(invalid)))
+ msg.empty();
+ widget.setStatus(
+ path in changed ? "changed" : null
+ );
+
+ delete invalid[path];
+ if (!(_.size(invalid)))
+ setStatus(
+ "changed",
+ "You have uncommitted changes",
+ true
+ );
+
+ }).fail(function(xhr) {
+ if (xhr.statusCode().status == 422)
+ msg.html(_.reduce(
+ _.map(
+ $.parseJSON(
+ xhr.responseText
+ ),
+ _.escape
+ ),
+ function(a, b) {
+ return a + "<br/>" + b;
+ }
+ ));
+ else showError(msg, "Error", xhr);
+
+ widget.setStatus("invalid");
setStatus(
- "changed",
- "You have uncommitted changes",
- true
+ "invalid",
+ "Some values need checking",
+ false
);
-
- }).fail(function(xhr) {
- if (xhr.statusCode().status == 422)
- msg.html(_.reduce(
- _.map(
- $.parseJSON(xhr.responseText),
- _.escape
- ),
- function(a, b) {
- return a + "<br/>" + b;
- }
- ));
- else showError(msg, "Error", xhr);
-
- widget.setStatus("invalid");
- setStatus(
- "invalid",
- "Some values need checking",
- false
- );
+ });
});
});