From 23f130da58e2ade555bb9e8de9572757fe68d9de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 23 Mar 2018 14:17:23 +0100 Subject: [PATCH] Fix "undefined" settings values not saving Fixes #2494 --- src/octoprint/static/js/app/client/base.js | 14 +++++++++++--- src/octoprint/static/js/app/helpers.js | 16 +++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/octoprint/static/js/app/client/base.js b/src/octoprint/static/js/app/client/base.js index 70d47da0e..2eb51d4d9 100644 --- a/src/octoprint/static/js/app/client/base.js +++ b/src/octoprint/static/js/app/client/base.js @@ -91,6 +91,14 @@ return params; }; + var replaceUndefinedWithNull = function(key, value) { + if (value === undefined) { + return null; + } else { + return value; + } + }; + OctoPrintClient.prototype.getBaseUrl = function() { var url = this.options.baseurl; if (!_.endsWith(url, "/")) { @@ -165,7 +173,7 @@ }; OctoPrintClient.prototype.postJson = function(url, data, opts) { - return this.post(url, JSON.stringify(data), contentTypeJson(opts)); + return this.post(url, JSON.stringify(data, replaceUndefinedWithNull), contentTypeJson(opts)); }; OctoPrintClient.prototype.put = function(url, data, opts) { @@ -173,7 +181,7 @@ }; OctoPrintClient.prototype.putJson = function(url, data, opts) { - return this.put(url, JSON.stringify(data), contentTypeJson(opts)); + return this.put(url, JSON.stringify(data, replaceUndefinedWithNull), contentTypeJson(opts)); }; OctoPrintClient.prototype.patch = function(url, data, opts) { @@ -181,7 +189,7 @@ }; OctoPrintClient.prototype.patchJson = function(url, data, opts) { - return this.patch(url, JSON.stringify(data), contentTypeJson(opts)); + return this.patch(url, JSON.stringify(data, replaceUndefinedWithNull), contentTypeJson(opts)); }; OctoPrintClient.prototype.delete = function(url, opts) { diff --git a/src/octoprint/static/js/app/helpers.js b/src/octoprint/static/js/app/helpers.js index 0781c9b6f..98a1e5741 100644 --- a/src/octoprint/static/js/app/helpers.js +++ b/src/octoprint/static/js/app/helpers.js @@ -911,15 +911,12 @@ function splitTextToArray(text, sep, stripEmpty, filter) { * and is optimized to check for value changes, not key updates. */ function hasDataChanged(data, oldData) { - if (data == undefined) { + // noinspection EqualityComparisonWithCoercionJS + if (data == oldData && data == undefined) { return false; } - if (oldData == undefined) { - return true; - } - - if (_.isPlainObject(data)) { + if (_.isPlainObject(data) && _.isPlainObject(oldData)) { return _.any(_.keys(data), function(key) {return hasDataChanged(data[key], oldData[key]);}); } else { return !_.isEqual(data, oldData); @@ -956,10 +953,12 @@ function hasDataChanged(data, oldData) { * and is optimized to check for value changes, not key updates. */ function getOnlyChangedData(data, oldData) { + // noinspection EqualityComparisonWithCoercionJS if (data == undefined) { return {}; } + // noinspection EqualityComparisonWithCoercionJS if (oldData == undefined) { return data; } @@ -972,17 +971,20 @@ function getOnlyChangedData(data, oldData) { var retval = {}; _.forOwn(root, function(value, key) { var oldValue = undefined; + // noinspection EqualityComparisonWithCoercionJS if (oldRoot != undefined && oldRoot.hasOwnProperty(key)) { oldValue = oldRoot[key]; } if (_.isPlainObject(value)) { + // noinspection EqualityComparisonWithCoercionJS if (oldValue == undefined) { retval[key] = value; } else if (hasDataChanged(value, oldValue)) { retval[key] = f(value, oldValue); } } else { - if (!_.isEqual(value, oldValue)) { + // noinspection EqualityComparisonWithCoercionJS + if (!(value == oldValue && value == undefined) && !_.isEqual(value, oldValue)) { retval[key] = value; } }