How to determine when Writeback is enabled for a Property?

What I’m trying to do:
Make a custom component work properly in an edge case. Currently, it writes data back on Hide. And when writeback is enabled for that property, that’s the obviously right thing to do.

Just as obviously, that should not happen when writeback is disabled for that property. But how do I tell, in the on-hide handler, that this particular instance of the component has writeback turned off?

Hi @p.colbert,
At the moment, there isn’t a way to directly check whether writeback is enabled for a property in code.
It might help if you could share a bit more about what you’re trying to achieve with your custom component. We might be able to suggest a different approach that avoids relying on checking writeback directly.

1 Like

More context, as requested!

This happens in a component which uses radio buttons to represent part of the data entered (one member in a dict).
image
We can’t bind rb.get_group_value() to a variable directly. Code must write the value back, and that code needs to be triggered reliably.

Even with writeback turned on
image
writeback wasn’t occurring. The user’s radio-button selection (if it changed) didn’t get preserved. So I triggered the writeback code on hide. This was very reliable.

I had been using this component, and other versions of it, exclusively in cases where writeback was enabled, so writing back unconditionally, in an on-hide handler, worked just fine.

Then, recently, I needed two different versions, on the same form. Worse, each handles a different view of the same dict’s data.

Fortunately, the two versions are mutually exclusive. Users see one, or the other, depending on a separate setting.
image

The visible one needs to write back, obviously. But the invisible one needs NOT to write back, to avoid overwriting the user-entered data with its (unchanged) copy.

Currently, as a workaround, I’m using a visibility check to keep the invisible component from writing back. If I was disabling components, instead of making them invisible, I could check enablement instead.

This works because the component’s visibility cannot change during the Form’s lifetime. Its initial copy of the data is always the copy that the end-user should see and edit.

I would like to know how to write components that use radio buttons, and directly respect the Writeback flag, instead of using other flags as a proxy.

But my workaround is adequate for now.

have you tried the anvil-extras RadioGroup component
writeback on selected_value should just work as you’d expect

I considered that, but my writeback has to fill in an entire dictionary – more than one value, represented on screen by more than just the radio button.

So, in order to properly set the writeback flag for the RadioGroup, I’d still have to be able to read the writeback flag for my own component.

I think a minimal example here would be helpful here, so we can take a look at the code

1 Like