diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-11-22 09:34:05 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-11-22 09:34:05 +0800 |
commit | b542121fa90041ea457dd605feaca7077a8f6bc4 (patch) | |
tree | 2ac49bd844c75061bc7599f59db9c277b3d7503b /fg21sim/webui/static | |
parent | 4d7072d5b15c7bb2a2260c4f1566950f6220fb38 (diff) | |
download | fg21sim-b542121fa90041ea457dd605feaca7077a8f6bc4.tar.bz2 |
webui: configs: Some minor fixes
* configs.py/_set_configs(): Also return the original data if the input
values failed the validation;
* setServerConfigs(): Success callback also reset value if the input
value failed pass the server-side validation;
* getServerConfigs(): Return "undefined" if key does not exists.
Diffstat (limited to 'fg21sim/webui/static')
-rw-r--r-- | fg21sim/webui/static/js/configs.js | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/fg21sim/webui/static/js/configs.js b/fg21sim/webui/static/js/configs.js index f7e3731..f321881 100644 --- a/fg21sim/webui/static/js/configs.js +++ b/fg21sim/webui/static/js/configs.js @@ -118,8 +118,9 @@ var resetFormConfigs = function () { * * @param {String} name - The name of filed name * - * @returns value - value of the option field - * + `null` if the field not exists or empty (no value) + * @returns value - value of the configuration option field + * + `null` if the field is empty (no value) + * + `undefined` if the field does not exists */ var getFormConfigSingle = function (name) { var value = null; @@ -143,8 +144,11 @@ var getFormConfigSingle = function (name) { value = target.filter(":checked").map( function () { return $(this).val(); }).get(); } else if (target.is(":text") && target.data("type") === "array") { - // Convert back to Array + // Convert comma-separated string back to Array value = target.val().split(/\s*,\s*/); + if (value.length === 1 && value[0] === "") { + value = []; // Empty Array + } } else { value = target.val(); } @@ -153,6 +157,7 @@ var getFormConfigSingle = function (name) { value = null; } } else { + value = undefined; console.error("No such element:", selector); } } @@ -241,11 +246,11 @@ var setFormConfigSingle = function (name, value) { var setFormConfigs = function (data, errors) { // Set the values of form field to the input configurations data $.each(data, function (name, value) { - if (value == null) { + if (value === null) { value = ""; // Default to empty string } var val_old = getFormConfigSingle(name); - if (val_old !== value) { + if (typeof val_old !== "undefined" && val_old !== value) { setFormConfigSingle(name, value); console.log("Set input '" + name + "' to:", value, " <-", val_old); } @@ -388,7 +393,7 @@ var setServerConfigs = function (url, data) { data = typeof data !== "undefined" ? data : {}; return $.postJSON(url, {action: "set", data: data}, function (response) { - setFormConfigs({}, response.errors); + setFormConfigs(response.data, response.errors); }) .fail(function (jqxhr) { var modalData = {}; |