From 0f66997b445d49799dd5a24f89f5e4be7dbdfc82 Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Tue, 21 May 2013 15:52:10 +0300 Subject: 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 --- web/client.js | 145 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 89 insertions(+), 56 deletions(-) (limited to 'web') 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 + "
" + 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 + "
" + b; - } - )); - else showError(msg, "Error", xhr); - - widget.setStatus("invalid"); - setStatus( - "invalid", - "Some values need checking", - false - ); + }); }); }); -- cgit v1.2.3