Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pushing of subfields #289

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions assets/js/watchers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
function getField(obj, indices) {
return indices.reduce((current, index) => current && current[index], obj);
}

function setField(obj, indices, value) {
const lastIndex = indices.length - 1;
const lastKey = indices[lastIndex];

indices.reduce((current, index, i) => {
if (i === lastIndex) {
current[lastKey] = value; // Set the value at the final index
} else {
if (!current[index]) {
current[index] = {}; // Create nested objects if they don't exist
}
}
return current[index];
}, obj);
}

const watcherMixin = {
methods: {
// Acknowledgement: copied watchIgnorable from VueUse
Expand Down Expand Up @@ -47,13 +67,17 @@ const watcherMixin = {
},

updateField: function (field, newVal) {
// allow for subindexing with dot notation
var fields = field.split('.')
field = fields[0]

if (field=='js_app') { newVal(); return }

try {
if (this['_ignore_' + field]) {
this['_ignore_' + field](()=>{this[field] = newVal});
this['_ignore_' + field](() => { setField(this, fields, newVal) });
} else {
this[field] = newVal
setField(this, fields, newVal)
}
if (field=='js_model' && typeof(this[field])=='function') {
this[field]()
Expand Down
Loading