Bindings ######## In the :doc:`module` and :doc:`component` sections, it has been explained that both can host a ``bindings`` declaration. These ``bindings`` are a link between: *Attributes* and *Observables* (see :doc:`/architecture/observables`) The declaration looks like this. .. code-block:: python from anpylar import Component, html ... class PyroesComponent(Component): bindinds = { 'pyroes': [], } ... This at first seems like a normal *dict* declaration containing a ``pyroes`` key and a matching value of ``[]`` (an empty *list*), but there is more. Because the declaration happens inside a subclass of ``Component`` the following holds true: - ``PyroesComponent`` will automatically have an attribute named ``pyroes`` which will obviously have a default value of ``[]`` - A 2nd attribute named ``pyroes_`` will be available and this is an **Observable** .. note:: Yes, the observable attribute receives the suffix ``_`` (an underscore). In Python ``_`` is usually doubled before and after a name to indicate a Python reserved name, doubled before a name to indicate a name-mangled component and used as single character before a name to indicate a kind of **reserved** attribute. *AnPyLar* has chosen to mark the *bindings* (or *observable attributes*) by using a single ``_`` as a **suffix** .. seealso:: If you are eager, you can also go straight to :doc:`/architecture/observables` Both attributes are linked together so that: - Setting the value of ``pyroes`` triggers the *observable* ``pyroes_`` and therefore any operations subscriptions to it - Setting (or *calling*) the *observable* sets the value of the attribute:: self.pyroes_([1, 2, 3]) # equivalent to self.pyroes = [1, 2, 3] Let's see it in code terms .. code-block:: python from anpylar import Component, html class Counter(Component): bindinds = { 'count': 0, } def render(self, node): html.h1('{}')._fmt(self.count_) with html.button('Count up!') as b: b._bindx('click', self.do_count) def do_count(self): # Alternative -> self.count_(self.count + 1) # Alternative -> self.count_ = self.count + 1 self.count += 1 .. note:: You can test this simply script with ``anpylar-serve`` without creating a complicated structure by placing the contents in a file ``index.py`` and doing:: anpylar-serve --auto-serve index.py With this basic example the powers of the binding (*attribute* <-> *observable*) could be explained: .. code-block:: python html.h1('{}')._fmt(self.count_) We are creating an ``

`` with the formatting template ``{}`` as text. This will be formatted to contain the value delivered by ``_fmt(self.count_)`` Because ``self.count_`` is an *Observable*, there will be a background subscription to it. Whenever the value of ``self.count`` changes, this will be reflected as a event through the observable and the value of our ``

`` tag will change. .. code-block:: python with html.button('Count up!') as b: b._bindx('click', self.do_count) We are now adding a ``