Live Chat

We'll need to share your messages (and your email address if you're logged in) with our live chat provider, Drift. Here's their privacy policy.

If you don't want to do this, you can email us instead at contact@anvil.works.

How Data Bindings work

Data bindings are actually very simple. If you set the binding on the self.component_1’s foo property to SOME_EXPRESSION, then when the refreshing_data_bindings event triggers on your form, we execute:

self.component_1.foo = SOME_EXPRESSION

(Setting self.item = [anything] on your form automatically triggers a data binding refresh; you can also do it manually with self.refresh_data_bindings().)

Writeback

If the foo property is enabled for writeback, then when self.component_1 raises an event that triggers that writeback (eg a CheckBox’s change event triggers writeback on its checked property), we execute this:

SOME_EXPRESSION = self.component_1.foo

Worked example

My form has self.item set to a row from the People table, which has a “name” column. I’ve got self.name_box’s textbound to self.item['name'], with writeback enabled.

  1. I set self.item = person_row. This triggers an automatic data-binding refresh.
  2. The data binding runs: self.name_box.text = self.item['name']. The name appears in the text box.
  3. I now edit the text box, and click away.
  4. The lost_focus event triggers data binding writeback on the text property of a TextBox
  5. The data binding runs: self.item['name'] = self.name_box.text. This updates the database.

This article was inspired by this discussion in our community forum, where we go into more detail about custom components too.

For a more detailed discussion, see Data Bindings in the reference docs.