form.update() populates 'initial' property in array field.
Hello. I have next fields.
fields: [
...
{
name: 'aliases',
label: 'Alias domains list',
value: [],
bindings: 'List',
options: {
validateOnChange: true
}
}
...
]
Custom binding is very simple and doesn't refer to the issue.
bindings() {
return {
List: {
id: 'id',
name: 'name',
type: 'type',
value: 'items',
label: 'label',
placeholder: 'placeholder',
disabled: 'disabled',
onChange: 'onChange',
onBlur: 'onBlur',
onFocus: 'onFocus',
autoFocus: 'autoFocus'
}
};
}
In a constructor of my component I do following:
this.form = new Form();
this.form.update(this.store.activeRule);
Active rule is an object with initial values. For example aliases property contains an observable array with values ['123', '456']. After this.form.update(this.store.activeRule); 'aliases' field contains initial array not only in value property, but in initial too.
Because of this when I do this.form.$('aliases').sync(aliases);, I can't update the field with new values. And it works fine when an initial array is empty.
I checked how form.update() works for other fields and saw that initial property wasn't populated.
I don't think that it is a correct behavior, but maybe I don't understand something.
I will be glad to any help.
Thanks in advance.
I'm seeing this same issue using a select input similar to a multi-select, but with the ability to add custom tags on the fly. The value property of this element takes an array, and if I give it an empty array as an initial value, it works fine. I can type into the input and it updates the associated field's value property by pushing new strings onto the array.
However, if I try to use form.update() or form.$('field').set(), the values appear in the input, but interaction with the input is totally broken. I can neither add ~~or~~ nor remove values from it.
If you pass an array to update() method, it will recreate the array, each element becomes a new field deleting the old elements (for this reason the initial prop is defined for each of the newly created nested fields). This approach will not work with a select input, as it doesn't work with nested fields (instead you need an array as the Field value prop, not nested fields).
Using set() (or using sync() which is an Event Handler) the value prop of the Field instance is updated (with the array provided in your case). It will not change the initial prop. This approach should work with the ANT select input (but it's not officially supported by the package, maybe you will need to implement a custom onChange(e) Event Handler).
@foxhound87 Thanks! Looks like set() does in fact work. Great library, by the way.
@foxhound87 I've used update() instead of sync() and it works. Thank you.
Great!