From ade3a772e44dbc3151d7ee93698edd539b6d1ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 7 Dec 2020 16:46:53 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Replace=20=5F.merge=20with=20obj?= =?UTF-8?q?ect=20only=20deepMerge?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It turns out _.merge will also merge arrays, which in turn will re-add anything we've just removed from a setting that happens to be an array, thus as blocked commands or temperature presets. Fixes #3855 Fixes #3867 --- src/octoprint/static/js/app/helpers.js | 24 +++++++++++++++++++ .../static/js/app/viewmodels/settings.js | 6 ++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/octoprint/static/js/app/helpers.js b/src/octoprint/static/js/app/helpers.js index cc2d7fc34..16ff6cd14 100644 --- a/src/octoprint/static/js/app/helpers.js +++ b/src/octoprint/static/js/app/helpers.js @@ -1592,3 +1592,27 @@ var loadFromLocalStorage = function (key) { } return current; }; + +var deepMerge = function (target, source) { + /** + * Implements an object deep merge, which contrary to _.merge doesn't try to + * merge arrays. + */ + if (!_.isObject(target)) { + return target; + } + + _.forOwn(source, function (value, key) { + if ( + target.hasOwnProperty(key) && + _.isPlainObject(target[key]) && + _.isPlainObject(value) + ) { + target[key] = deepMerge(target[key], value); + } else { + target[key] = value; + } + }); + + return target; +}; diff --git a/src/octoprint/static/js/app/viewmodels/settings.js b/src/octoprint/static/js/app/viewmodels/settings.js index 8a4c341bc..c81556e15 100644 --- a/src/octoprint/static/js/app/viewmodels/settings.js +++ b/src/octoprint/static/js/app/viewmodels/settings.js @@ -1142,7 +1142,7 @@ $(function () { // process all key-value-pairs here _.forOwn(data, function (value, key) { var observable = key; - if (keyPrefix != undefined) { + if (keyPrefix !== undefined) { observable = keyPrefix + "_" + observable; } @@ -1161,7 +1161,7 @@ $(function () { mapping && mapping[key] ? mapping[key] : undefined, observable ); - if (subresult != undefined) { + if (subresult !== undefined) { // we only set something on our result if we got something back result[key] = subresult; flag = true; @@ -1179,7 +1179,7 @@ $(function () { // map local observables based on our existing data var dataFromObservables = mapFromObservables(data, specialMappings); - data = _.merge(data, dataFromObservables); + data = deepMerge(data, dataFromObservables); return data; };