Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

RefreshingItem #38

Closed
meatballs opened this issue Mar 24, 2021 · 5 comments
Closed

RefreshingItem #38

meatballs opened this issue Mar 24, 2021 · 5 comments
Assignees
Labels
enhancement New feature or request idea A potential idea utility add a utility function or class

Comments

@meatballs
Copy link
Member

A client side class for use as a form's item attribute.

It exposes a dict like interface but calls the form's refresh_data_bindings method whenever it's updated.

It will need to take a form instance as one of its init arguments.

It could provide an option to behave like a defaultdict.

Prompted by https://anvil.works/forum/t/data-binding-not-refreshing/7923/4

@s-cork s-cork added enhancement New feature or request idea A potential idea utility add a utility function or class labels Mar 25, 2021
@s-cork
Copy link
Collaborator

s-cork commented Mar 25, 2021

Maybe there's a way to do it without the need to know the form in advance

Pseudo code:

# Some Form class
from anvil_extras import FormItemListener

class ItemTemplate(ItemTemplateTemplate):
  def __init__(self, **properties):
    FormItemListener.init(self)
# FormItemListener

_classes = set()

def _item_override(cls):
  base_item = super(cls, cls).item
  
  def item_getter(self):
    return base_item.__get__(self, cls)
  
  def item_setter(self, item):
    item = _cls_for_refreshing_item(item)
    item._form = self
    base_item.__set__(self, item)
  
  return property(item_getter, item_setter)


def init(form):
  cls = type(form)
  if cls in _classes:
    return
  else:
    cls.item = _item_override(cls)
    _classes.add(cls)

_dict_setitem = dict.__setitem__ # save an attribute lookup on each call to __setitem__

class _D(dict):
  def __setitem__(self, key, value):
    _dict_setitem(self, key, value)
    form = getattr(self, '_form', None)
    if form is not None:
      form.refresh_data_bindings()

_cls_for_refreshing_item = _D

proof of concept:
https://anvil.works/build#clone:RVJVNFVQDKC6IWLH=DS7FRFDKYMHUPUIN7K5HCNIW

@s-cork
Copy link
Collaborator

s-cork commented Mar 25, 2021

In the proof of concept i've suggest 3 possible apis

# Some Form class
from anvil_extras import FormItemListener

class ItemTemplate(ItemTemplateTemplate):
  def __init__(self, **properties):
    FormItemListener.init(self)
# Some Form class
from anvil_extras import FormItemListener

class ItemTemplate(FormItemListener.BaseClass, ItemTemplateTemplate):
# Some Form class
from anvil_extras import FormItemListener

class ItemTemplate(ItemTemplateTemplate):
   ...

FormItemListener.init_class(ItemTemplate) # or equivalent decorator

I think option 1 or 3 are best...

@meatballs
Copy link
Member Author

Excellent! I went to bed last night thinking there must be a way to do this with a decorator on the Form class and I wake up to find the code for that has magically appeared!

@meatballs
Copy link
Member Author

I'm happy to pick this up from here (but also happy for you to carry on if you prefer).

@s-cork
Copy link
Collaborator

s-cork commented Mar 25, 2021

go for it.

@meatballs meatballs self-assigned this Mar 25, 2021
meatballs added a commit that referenced this issue Mar 25, 2021
meatballs added a commit that referenced this issue Mar 26, 2021
meatballs added a commit that referenced this issue Mar 26, 2021
meatballs added a commit that referenced this issue Mar 26, 2021
meatballs added a commit that referenced this issue Mar 26, 2021
meatballs added a commit that referenced this issue Mar 26, 2021
@s-cork s-cork closed this as completed Mar 27, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request idea A potential idea utility add a utility function or class
Projects
None yet
Development

No branches or pull requests

2 participants