Fix setting options in array of objects (#9062)

* Fix setting options in array of objects

* CC
This commit is contained in:
Jukka Kurkela 2021-05-12 23:08:17 +03:00 committed by GitHub
parent 024f406223
commit f1ca99bfd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 7 deletions

View File

@ -290,13 +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] = {};
}
return parent[prop];
});
return _createResolver([...set], [''], rootScopes, fallback,
() => subGetTarget(resolver, prop, value));
}
function addScopesFromKey(set, allScopes, key, fallback) {
@ -306,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) {

View File

@ -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 = {