From 8552d5bb37d83807c8f4d01f160e1fe58d361fb2 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Tue, 11 May 2021 08:23:31 +0300 Subject: [PATCH 1/2] Fix setting options in array of objects --- src/helpers/helpers.config.js | 7 ++++++- test/specs/helpers.config.tests.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/helpers/helpers.config.js b/src/helpers/helpers.config.js index 1c70d7a780c..1404b238786 100644 --- a/src/helpers/helpers.config.js +++ b/src/helpers/helpers.config.js @@ -295,7 +295,12 @@ function createSubResolver(parentScopes, resolver, prop, value) { if (!(prop in parent)) { parent[prop] = {}; } - return parent[prop]; + const target = parent[prop]; + if (isArray(target) && isObject(value)) { + // For array of objects, the object is used to store updated values + return value; + } + return target; }); } diff --git a/test/specs/helpers.config.tests.js b/test/specs/helpers.config.tests.js index 2c2da96359b..811d386d303 100644 --- a/test/specs/helpers.config.tests.js +++ b/test/specs/helpers.config.tests.js @@ -752,6 +752,23 @@ describe('Chart.helpers.config', function() { expect(fn()).toEqual('ok'); }); + it('should properly set value to object in array of objects', function() { + const defaults = {}; + const options = { + annotations: [{ + value: 10 + }, { + value: 20 + }] + }; + const resolver = _attachContext(_createResolver([options, defaults]), {test: true}); + expect(resolver.annotations[0].value).toEqual(10); + + resolver.annotations[0].value = 15; + expect(options.annotations[0].value).toEqual(15); + expect(options.annotations[1].value).toEqual(20); + }); + describe('_indexable and _scriptable', function() { it('should default to true', function() { const options = { From 9432346c4e60077b2ed93cba8ecab8137be5ce90 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Tue, 11 May 2021 08:45:40 +0300 Subject: [PATCH 2/2] CC --- src/helpers/helpers.config.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/helpers/helpers.config.js b/src/helpers/helpers.config.js index 1404b238786..a3625984daa 100644 --- a/src/helpers/helpers.config.js +++ b/src/helpers/helpers.config.js @@ -290,18 +290,8 @@ function createSubResolver(parentScopes, resolver, prop, value) { return false; } } - return _createResolver([...set], [''], rootScopes, fallback, () => { - const parent = resolver._getTarget(); - if (!(prop in parent)) { - parent[prop] = {}; - } - const target = parent[prop]; - if (isArray(target) && isObject(value)) { - // For array of objects, the object is used to store updated values - return value; - } - return target; - }); + return _createResolver([...set], [''], rootScopes, fallback, + () => subGetTarget(resolver, prop, value)); } function addScopesFromKey(set, allScopes, key, fallback) { @@ -311,6 +301,19 @@ function addScopesFromKey(set, allScopes, key, fallback) { return key; } +function subGetTarget(resolver, prop, value) { + const parent = resolver._getTarget(); + if (!(prop in parent)) { + parent[prop] = {}; + } + const target = parent[prop]; + if (isArray(target) && isObject(value)) { + // For array of objects, the object is used to store updated values + return value; + } + return target; +} + function _resolveWithPrefixes(prop, prefixes, scopes, proxy) { let value; for (const prefix of prefixes) {