I have a single-page application. I would like to call a server function, specifically to store a cookie, only when the user closes the page. Is there a way to do this?
I’ve spent a while looking through the Docs and Forum without finding anything that seems to be what I’m looking for.
I did find this question but I don’t understand the code well enough to know if this is what I want.
The last post in the thread you mentioned shows how to define the event, register it when the form is shown and unregister it when it’s hidden. All in python, without bothering javascript.
But before you go that way, I would like to know why you are trying to use cookies. I have hundreds of apps and never use cookies. Perhaps there is a better Anvil way to do what you are trying to do.
To the first part of your response, I believe I understand that, thanks.
In terms of cookies, I’m storing the user’s settings in between sessions. Here is my app so you can see what I mean. It stores the user’s pattern if they’ve selected to do so with the checkbox.
Calls to the server make the page briefly reload, which is distracting and annoying for a user, so I’ve currently elected to run a check every 30 seconds (if the checkbox is checked) to see if the pattern has been changed.
I can’t see any other way to store a specific variation of a setting per user like this… perhaps you will know a better way?
If per browser, that is user X needs to remember different settings on different browsers, then cookies could work, but I don’t like them because they are not flexible and they send data back and forth. I like to use local storage, more flexible than cookies. Anvil Extras provides an easy interface for it, here is how I use it: Persist state of form when navigating away from it - #6 by stefano.menci
If per user, that is user X should get the same settings on all browsers, then you can add a “settings” column to the Users table, then use users.get_user()['settings'].
Calls to the server do not make the page reload. If you use call_s instead of call, you don’t even see the spinner. Most of my apps have timers that call the server every few seconds to see if something is changed and update the ui accordingly, and there is no visible effect on the browser.
If you want to store the settings in the Users (or any other) table, I would call a server function to do it every time any setting changes. There may be many reasons why the user doesn’t close the page and the setting is not saved, and it may be too difficult and unreliable.
Per browser would be more correct. I’m not doing anything to make the experience seamless for a single user across different browsers. Thanks for the tip on users.get_user()['settings'].
Ah, yes the spinner is what I meant. call_s is great, I’ll just use that for now to hide the server calls .
Yes, I’ve set it up now so that an update happens anytime the checkbox is toggled, and a timer that runs every few seconds to check for changes to the pattern - saving them if there are… rather than waiting for the user to close the page. I realised that going that route would be more volatile and likely lead to changes not being saved.