Component text is updated unexpectedly (following a false condition)

Hello Everyone,

I’m trying to figure out why an Item is updating values when I cancel the Edit of the item.

I just use “self.refresh_data_bindings()” when the alert returns “True”. So it wouldn’t happen when i returned other value whatever it is. But somehow it still binding the update. Do i need to change the way it’s updated? Or is it another problem that I’m not seeing?

Another question is why it doesn’t show the new data text

https://anvil.works/build#clone:J47XUN4CKKTJZQUR=X3RRNZPIOFVWWHOWBJNXTJTC

Can anyone clarify why this happens??

Thanks in Advance,
João Luis.

Hi João Luis, welcome to the Forum!

This is to do with a detail of how Data Bindings work, that I need to document better!

The write_back part of Data Bindings is triggered by certain input events. The TextBox and TextArea run the writeback on pressed_enter or lose_focus. So the data has already been written back to the Data Table before the alert is closed.

You’ll need to uncheck write_back, and explicitly specify what you want to happen when the alert is closed.

Here’s a modified version of your app. I’ve created a save_changes event on your Edit Form:

class Edit(EditTemplate):
  def __init__(self, **properties):
    # ...
    self.set_event_handler('x-save-changes', self.save_changes)

  def save_changes(self, **event_args):
    self.item['name'] = self.text_box_1.text
    self.item['desc'] = self.text_area_1.text

and I’m triggering it if the alert closes with True:

     if ret == True:
      edit_form.raise_event('x-save-changes')
      self.refresh_data_bindings()

https://anvil.works/build#clone:4NNM73JTUVNZX3P3=FINMRTUOOVIAFYRSP2B6CWKD

I’ll update our documentation to make the behaviour of write_back clearer. Thanks for mentioning this, posts like this help us improve our documentation.

Cheers,
Shaun

1 Like

Hey Shaun,

Thanks for the quick answer!!
Very clarifying!
I got the point.

I’m still a newb at Anvil, so I don’t know how a lot of stuff works, yet!
Anvil is AWESOME!

1 Like