brutusin / brutusin/json-forms

dependsOn in array is not working properly with resolver

Open
#107 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
618
Forks
171
PR merge metrics
No merged PRs in 30d

Description

Hi,

I've created a Form with couple of array's. One of them has three dropdown where the second depends on the first and the third on the second.
I've created a resolver for this to get the right values for the dropdowns from a existing hierarchical object with three dimensions.
This is working like expected so far.
But the datastore is not updated like it should.

After I added the first item in the array, all three values are present in the datastore if I check them with getData(). Then with adding the next item, the issue starts. After adding the item to the array the previous entry in the datastore has no property two and three anymore, booth are deleted by adding the next item to the array.

First item added:
`{
"gender": "Female",
"services": [
{
"category": "Beauty & Wellness",
"service": "Beauty Salon",
"specialization": "Nail Salon"
}
]
}`

Next Item added
`{
"gender": "Female",
"services": [
{
"category": "Beauty & Wellness"
},
{
"category": "Beauty & Wellness",
"service": "Hairdresser"
}
]
}`

And this repeats on every adding. Only the last added item has the properties two and three the previous one are losing the properties.

Here is my resolver:
` const resolver = function(names, data, cb) {
let schemas = [];
let schema = [];

let i = data.services.length - 1;
let category = data.services[i].category;
let serviceDef = false;
if (names[0] === '$.services[#].specialization' && data.services[i].hasOwnProperty('service')) {
serviceDef = data.services[i].service;
}

let serviceObj = [];

if (names[0] === '$.services[#].service' && services[category]) {
schema.type = "string";
Object.keys(services[category]["services"]).forEach(function(key, value) {
serviceObj.push(key);
},services[category]["services"]);
} else if (serviceDef && services[category]['services'][serviceDef]) {
schema.type = "string";
services[category]['services'][serviceDef]["specializations"].forEach(function(key, value) {
serviceObj.push(key);
});
}
if (serviceObj.length === 0) schema.type = null;
schema.enum = serviceObj;
schemas[names[0]] = schema;
setTimeout(function(){cb(schemas)},500);
};
`

This resolver handles booth dropdown dependencies. I tried already to use the selector '$.services[$].service' instead of '$.services[#].service', so I thought this will just catch the last element in the array, but with this the dependencies are not builded correctly anymore.

I hope I can manage to set up a jsfiddle environment with the whole setting, so it will be easier to check it. Maybe it's also a problem in my resolver, maybe I set a wrong schema here?

Any help will be appreciated.

cheers

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.