Skip to content

base_component

django_spire.core.components.scroll.base_component

SCROLL_TEMPLATES = frozenset({'django_spire/component/scroll/scroll.html'}) module-attribute

ITEM_TEMPLATES = frozenset({'django_spire/component/scroll/item/item.html', 'django_spire/component/scroll/item/table_item.html'}) module-attribute

BATCH_TEMPLATE = 'django_spire/component/scroll/scroll_batch.html' module-attribute

ScrollBatch dataclass

items instance-attribute

keys instance-attribute

has_more instance-attribute

rows property

BaseScrollComponent

Bases: Component, ABC

An infinite list of any kind of item.

Subclasses supply the items through get_items(), get_item() and get_item_key(); this base owns paging and every client behaviour. The server keeps no position: the client sends the number of items it holds, and a render always shows the first batch.

item_template is the markup for one item. Each item is rendered on the server, so the template must extend the scroll's item/item.html or item/table_item.html.

A callable that changes an item fires item_added, item_changed or item_removed with key=, and the list updates that one row.

Served by as_view(), the list fills spire's full page.

template = 'django_spire/component/scroll/scroll.html' class-attribute instance-attribute

view_template = 'django_spire/component/page/full_page.html' class-attribute instance-attribute

item_template = None class-attribute

batch_size = 25 class-attribute

item_added = Glue.event() class-attribute instance-attribute

item_changed = Glue.event() class-attribute instance-attribute

item_removed = Glue.event() class-attribute instance-attribute

renders_items_on_server property

first_batch cached property

get_items abstractmethod

Return up to limit items starting at offset, in an order that is the same on every call.

Source code in django_spire/core/components/scroll/base_component.py
@abstractmethod
def get_items(self, offset: int, limit: int) -> list[Any]:
    """
    Return up to ``limit`` items starting at ``offset``, in an order that
    is the same on every call.
    """

get_item abstractmethod

Return the item with this key, or None when it no longer belongs in the list.

Source code in django_spire/core/components/scroll/base_component.py
@abstractmethod
def get_item(self, key: Any) -> Any | None:
    """Return the item with this key, or ``None`` when it no longer belongs in the list."""

get_item_key abstractmethod

Return the value that identifies item among all items.

Source code in django_spire/core/components/scroll/base_component.py
@abstractmethod
def get_item_key(self, item: Any) -> Any:
    """Return the value that identifies ``item`` among all items."""

introduce

Check, once per class, that its templates extend the scroll's own.

Source code in django_spire/core/components/scroll/base_component.py
def introduce(self, request: HttpRequest) -> None:
    """Check, once per class, that its templates extend the scroll's own."""
    scroll_class = type(self)

    if scroll_class not in self._validated_classes:
        self._validate_templates()
        self._validated_classes.add(scroll_class)

    super().introduce(request)

load_items

Source code in django_spire/core/components/scroll/base_component.py
@Glue.attr(skip_rerender=True)
def load_items(self, request: HttpRequest, offset: int) -> GlueResponse:
    self._refuse_unless_rendered_on_server()
    self._validate_offset(offset)

    return GlueTemplateResponse(
        request,
        BATCH_TEMPLATE,
        {
            'batch': self._get_batch(offset),
            'component': self,
        },
    )

load_item

Source code in django_spire/core/components/scroll/base_component.py
@Glue.attr(skip_rerender=True)
def load_item(self, request: HttpRequest, key: int | str) -> GlueResponse | None:
    self._refuse_unless_rendered_on_server()

    item = self.get_item(key)

    if item is None:
        return None

    return GlueTemplateResponse(
        request,
        self.item_template,
        {
            'item': item,
            'item_key': self.get_item_key(item),
            'component': self,
        },
    )