diff --git a/CHANGELOG.md b/CHANGELOG.md index 220b1fb..be1f3f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 1.4.0-alpha.8 (WIP) * Fix `_modelInstance` cache when keys are not strings +* Fix certain settings not being applied if they're created after generating the main system setting object ## 1.4.0-alpha.7 (2024-10-10) diff --git a/lib/app/model/system_setting_model.js b/lib/app/model/system_setting_model.js index c6e6f44..7b1cb1c 100644 --- a/lib/app/model/system_setting_model.js +++ b/lib/app/model/system_setting_model.js @@ -144,6 +144,7 @@ SystemSetting.setDocumentMethod(function applySetting(do_actions = true) { if (!existing_value) { existing_value = setting.generateValue(); + alchemy.system_settings.forceValueInstanceAtPath(this.setting_id, existing_value); } if (do_actions) { diff --git a/lib/core/setting.js b/lib/core/setting.js index 082ee4b..278b44f 100644 --- a/lib/core/setting.js +++ b/lib/core/setting.js @@ -452,6 +452,9 @@ const Group = Function.inherits('Alchemy.Setting.Base', function Group(name, con // All the children this.children = new Map(); + + // Weak references to existing values + this.weak_values = new Blast.Classes.WeakValueSet(); }); /** @@ -583,6 +586,14 @@ Group.setMethod(function createGroup(name) { let group = new Group(name, this); this.children.set(name, group); + if (this.weak_values.size) { + let group_value = group.generateValue(); + + for (let existing of this.weak_values) { + this.setDefaultValue(existing, {[name]: group_value}); + } + } + return group; }); @@ -633,6 +644,8 @@ Group.setMethod(function generateValue() { this.setDefaultValue(result, object); + this.weak_values.add(result); + return result; }); @@ -1344,6 +1357,38 @@ GroupValue.setMethod(function _setPath(silent, path, raw_value) { return this.getPath(path); }); +/** + * Force a value at the given path + * + * @author Jelle De Loecker + * @since 1.4.0 + * @version 1.4.0 + * + * @param {string|Array} path + * @param {Value} + */ +GroupValue.setMethod(function forceValueInstanceAtPath(path, value) { + + if (typeof path == 'string') { + path = path.split('.'); + } + + if (this.definition.group == null && path[0] == this.definition.name) { + path.shift(); + } + + let last = path.pop(); + + let current = this; + + while (path.length && current) { + let next = path.shift(); + current = current.get(next); + } + + current[VALUE][last] = value; +}); + /** * Convert to a datasource array * @@ -1584,6 +1629,20 @@ Value.setMethod(function getPath(path) { return current; }); +/** + * Force a value at the given path + * + * @author Jelle De Loecker + * @since 1.4.0 + * @version 1.4.0 + * + * @param {string|Array} path + * @param {Value} + */ +Value.setMethod(function forceValueInstanceAtPath(path, value) { + throw new Error('Unable to perform on a simple Value instance'); +}); + if (Blast.isBrowser) { return; }