I am trying to use auto-binding to save numeric values to my database.
My scenarios is:
I have three input fields (one for price, one for quantity and the last one for discount percentage) and they have auto-binding enabled.
Now I would like to add a fourth input which would display the total and save it automatically to my database (E.g. Total = (price* quantity) - (price * quantity/100* discount percentage) ).
Hi there @ljf…
Yes, I have an idea… which is not to use autobinding.
I played with it for ages and came to the conclusion it has all sorts of problems in real world use. I just avoid it completely.
I would try something like this. Create a custom event to calculate the total from your input values. This can be saved directly to the database in a make changes to a thing action.
Trigger this event when any of the three inputs has their value changed (and all three inputs have values).
Display the total from the database in your fourth input (or you can use a text element).
I agree with @antony though, that autobinding should be avoided where possible.
The only reason why I have had to use auto-binding up to now is because those inputs are inside a repeating group. I have only one button, underneath the repeating group, which has to save everything to database.
Unfortunately there is no way when clicking this button to use “create a new thing…” and getting the values of the inputs, because they are inside the repeating group.
Hi @ljf… I have moved to putting a save button in each cell of the RG, and having a condition on it which makes it light up if data has changed which needs saving. A bit more work for the user, but much much more reliable in the way Bubble works.
Hi @ljf, I think I might have a solution for that. You can create an input, then this input should not be visible on page load. Initial value of this input should be whatever you want to save, then enable auto binding. This should work.
Hi @ljf, this is what I did, when the value of any of the three inputs change, you run a workflow to create the “total”, this is easily done using the expression, only that this time round you wont have to use the input’s value, because they’re already saved in the database (autobinding).
Hi @antony, could you explain a bit more what you mean by this. I have had great success implementing autobinding for certain functionality but was just wondering if I should be anticipating any sorts of problems that I may not have noticed yet by relying on it.
Sometimes the value takes a while to update in the database if you have other things going on in a similar time frame. For me, this meant I could end up opening a popup, changing a value, closing and re-opening the popup, and I saw the old value as the database had not been updated.
If you have values that depend on each other, things can get very confusing. So if X=A+B and Y=C+X, the way Y gets updated was not clear and sometimes seemed to be just wrong.
Hence I decided to move away from auto-binding as a generic way of saving things, and just use it here and there for very simple situations.
Thanks for your detailed response! I think auto binding will still perform well for the simpler use cases I have used them for but this provides some good context about potential limitations.