Yes, Custom component property data binding IS possible when property is of type "object"

I have created a custom component intended for editing lists of numbers. Naturally, number_list is one of its formal properties. It is of type object.

number_list can be set at run-time. I provided the necessary functions and decorators to make it happen. When the main form sets this instance property to a list, the component receives and edits the list as desired.

The data gets into the component instance. I can see it in the component instance when the instance runs. I can change list entries, or compute an entirely new list, in the instance, and the instance behaves as intended.

Requesting the value of the property returns the revised list, even if the list has been completely recomputed and reasssigned. So far, so good.

But that doesn’t test data binding for number_list. To that end, I made some changes:

  1. In the main form (which embeds an instance of the custom component), I establish
    self.item = { 'nl': [5] }
  2. In the IDE, for the custom component instance, I create a data binding for property number_list as
    self.item['nl']

Again, the component receives and edits the data as designed. But nothing was written back to self.item['nl'].

So I set number_list to allow writeback. This requires that the component define its own event (I named it writeback). No further requirements on the event are documented. However, I discovered (from building a previous custom component) that you must attach a handler to the event. The handler itself doesn’t have to do anything. Apparently, the run-time environment does all the work.

(The component code can’t, obviously. It has no access to the bound expression. Indeed, for some instances, you may choose not to bind that property at all.)

With writeback thus enabled in the component, and turned on in the main form (yes, I checked), writeback does not occur. Even after editing the list in the custom component, the main form continues on with its own, original copy of the list.

Is writeback simply not implemented for properties of type ‘object’? Or is there some special step that my code needs to take, above and beyond what is documented, to write the list back?

Solved: I needed to
self.raise_event('writeback')
once the component’s internal changes (to the list) were complete.

1 Like

Glad to see you sorted this out! If you’re getting your head around Data Bindings, you might find the following brief explanation helpful (it explains eg, how data binding writeback on a component is triggered when that component raises the relevant event):

There’s a more in-depth description in our reference documentation:

https://anvil.works/doc#data_bindings