What I’m trying to do:
Set a property on a portable class using a server method
What I’ve tried and what’s not working:
server methods that manipulate self don’t seem to do anything when called from client.
I have to return something from the server method and then set that property from outside of the method.
stucork
2
to get the portable class to sync across server/client
you will need to use the capability update mechanism
this is how anvil table rows work
see this example from the docs
Note this means you will probably want to create the object on the server
since you can’t create a capability on the client
Minimal Example
import anvil.server
from uuid import uuid4
@anvil.server.portable_class
class Person:
def __init__(self, name):
self.name = name
self.id = str(uuid4())
self.cap = anvil.server.Capability(["Person", self.id])
self.cap.set_update_handler(self._handle_cache_update)
def __deserialize__(self, data, global_data):
self.__dict__.update(data)
self.cap.set_update_handler(self._handle_cache_update)
@anvil.server.server_method
def set_name(self, name):
print(name)
self.name = name
self.cap.send_update({"name": name})
def _handle_cache_update(self, update):
if "name" in update:
self.name = update["name"]