Coder Social home page Coder Social logo

RefreshingItem about anvil-extras HOT 5 CLOSED

anvilistas avatar anvilistas commented on September 25, 2024
RefreshingItem

from anvil-extras.

Comments (5)

s-cork avatar s-cork commented on September 25, 2024 1

go for it.

from anvil-extras.

s-cork avatar s-cork commented on September 25, 2024

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

from anvil-extras.

s-cork avatar s-cork commented on September 25, 2024

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...

from anvil-extras.

meatballs avatar meatballs commented on September 25, 2024

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!

from anvil-extras.

meatballs avatar meatballs commented on September 25, 2024

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

from anvil-extras.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.