Arduino HomeDing Blog Archive

Thoughs and records on IoT, Web Sites and Microprocessors.

Data binding in Web components

Layered Architecture with data layer

Using data in a web application that uses web components is an art on its own. Many of the current application frameworks support data bound UI by supporting a common data layer and mechanisms to interact with this data.

Again size matters as the SFC framework was also created to support environments with massive size restrictions like IoT Devices where the total file size for the web application needs to fit into about 1 MByte or less.

Here a minimal but extendable approach is implemented to create component based frontend applications that can deal with structured data without the burden of full fledged and feature rich frameworks like RxJS that require > 100 kB of scripts.

Background and Why

The implementation can be found in data-hub.ts on GitHub and is part of the Single File Components (SFC) project framework. It can be loaded into the client independently from the sfc loader as a ESM module.

The patterns used as a data-layer for web applications today mostly use a variation of reactive programming. The data layer that can store data, that the data model is accessible by components and that data changes trigger activities.

Both, the Observer Pattern and Publish / Subscriber Pattern are resulting in event driven implementations that differ in the underlying technical implementation but not much in the logic that needs to be implemented.

Using a simplified Publish/Subscriber implementation for loosely coupled, event driven interfaces was chosen over a well known non-native Observer implementation was chosen for size reasons.

The functionality that comes with the data store (all of this decoupled behind an appropriate API) that supports all of the above and includes also the detection of change to avoid useless updates, messages and infinity loops.

In many cases data is not flat but has a structure that fits to the problem. Imagine an application with presents data in tables (array) or multiple forms or cards (structure).

Client Side Architecture

The following picture illustrates how the data enabled custom components integrate with HTML elements and the data layer including the data hub and the data storage implementation.

Client Side Architecture

In the Custom Element Classes the Data Binding to the HTML elements is implemented by reading and writing attributes like shown values or style attribute changes.

Changes in the data and user interactions events will trigger callbacks that can be scoped to parts of the overall data model in the subscriber call. This will result in an event driven implementation of the display logic and can be combined with a declarative approach to bind the relevant data attributes.

Data Layer

The data layer covers the responsibility for storing structured data, data changes and the related change events but also acts as a universal usable data storage for structured data.

It implements:

Data Binding

Implementing components therefore should include some parameters or declarative attributes to bind to specific data attribute(s). What a component does with this information is usually obvious:

Input binding

See also

Tags

#sfc