neomjs / neo

The application worker driven frontend framework
https://neomjs.com
MIT License
2.83k stars 151 forks source link

model.Component: enhance 2way-bindings #5591

Closed tobiu closed 1 month ago

tobiu commented 1 month ago

Right now, 2way only works in case the name of the config is exactly the same as the name of the bound data prop:

        bind: {
            intlFormatDay: {twoWay: true, value: data => data.intlFormatDay},
            weekStartDay : {twoWay: true, value: data => data.weekStartDay}
        },

This was obviously not intentional.

We should evaluate if we want to keep the current syntax or modify it. 2way kind of enforces to only allow direct bindings and no field combinations, since we can not know how to transform back out of the box.

So we would either use the regex-parsing inside VMs or adjust the syntax. e.g.:

        bind: {
            weekStartDay: {twoWay: true, property: 'data.foo.bar.baz'}
        },
marklincoln commented 1 month ago

I think the new syntax would be better unless the prior syntax enables flexibility in combining field values to produce a calculated value. Obviously, this calculation could be done in another way prior to the binding so this flexibility might not be necessary. One question about the new syntax seems unclear: How does it bind to a stock config like the "value" property of the TextField?

tobiu commented 1 month ago

the properties (keys) inside the bind object are the names of the configs. for one way bindings, we do need the transformation function. using fat arrows is optional.

marklincoln commented 1 month ago

so, with the new syntax, I could do this?
bind: { value: {twoWay: true, property: 'data.foo.bar.baz'} },

tobiu commented 1 month ago

I would like to close this ticket, since the first version is working fine.

You can test it inside Neo.examples.model.twoWay.

I did test the other approach a bit, but this one is complicated: we would need to use new Function('data', `data.${value.property}`)

inside createBindings(), but also adjust parseConfigs().

Some security issues and time consuming.

You are welcome to create a follow-up ticket though, in case you think it is important.