Simple and lightweight solution for binding data to DOM elements.
This manual is also available in HTML5.
Data context binding library for browser.
This library is a simple and lightweight solution for binding data to DOM elements.
Used when building a single-page application (SPA), this library offers a modern approach.
frontend workflow: data >
'data-context' >
'data-context-binding' >
DOM elements
backend to frontend workflow: file >
'fs-broker' >
'data-context' >
'ws-user' >
'tiny-https-server' >
'ws13' >
browser >
'ws-user' >
'data-context' >
'data-context-binding' >
DOM elements
Please note, this version is not backward compatible with version 1.x
Requires 'data-context' module version 2.x
Note the restrictive requirements of the 'data-context' module.
To test, open the 'index.test.html' file. The tests will run and the results will be displayed.
or on the command line in the project directory 'data-context-binding':
npm test
This example demonstrates a simple and effective way to bind data to DOM elements using the data-context-binding library.
Summary
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>data-context-binding</title>
<!-- STEP 1. Import the modules 'data-context' and 'data-context-binding'.
Import for an HTML page hosted on the server. -->
<!--<script src="./datacontext.js"></script>-->
<!--<script src="./browser.js"></script>-->
<!-- STEP 1. Import the modules 'data-context' and 'data-context-binding'.
Import for a standalone HTML page. -->
<script async src="https://cdn.jsdelivr.net/npm/data-context"></script>
<script async src="https://cdn.jsdelivr.net/npm/data-context-binding"></script>
<!-- STEP 2. Add the data to a container with the ID 'data'. Its contents will be read automatically. -->
<script id="data" type="application/json">
{
"doc": {
/* 1: The metadata-title context object */
/* 2: The metadata-title context object */
"title": "Hello World", // The title comment
/* The metadata-description context object */
"description": "This is a simple example of data context binding.", // The description comment
/* { "hidden": "This is a must be hidden value." } */
"debug": false
}
}
</script>
</head>
<body>
<!-- STEP 3. Bind data to the DOM elements.
In this container we use the 'doc' object.
The 'path' attribute is part of property path. -->
<div path="doc">
<div>
<!-- STEP 3. Bind data to the DOM elements.
The `path` attribute is property path in the data object.
The `bind` attribute is performed data binding. -->
<h1 path="title" bind>Title</h1>
<!-- STEP 3. Bind data to the DOM elements.
The `path` attribute is property path in the data object.
The `bind` attribute is performed data binding. -->
<p path="description" bind>Description</p>
</div>
</div>
<hr />
<h3>Edit the values:</h3>
<label for="title">Title:</label>
<br />
<!-- STEP 3. Bind data to the DOM elements.
The `path` attribute is property path in the data object.
The `bind` attribute is performed data binding. -->
<input type="text" path="doc.title" bind style="width:380px" />
<br />
<br />
<label>Description:</label>
<br />
<!-- STEP 3. Bind data to the DOM elements.
The `path` attribute is property path in the data object.
The `bind` attribute is performed data binding. -->
<input type="text" path="doc.description" bind style="width:380px" />
<hr />
<label>Live changes:</label>
<br />
<!-- STEP 3. Bind data to the DOM elements.
The `bind` attribute is performed data binding.
Listens for data context changes -->
<textarea cols="50" rows="10" bind="change"></textarea>
</body>
</html>
Here is also a DEMO
This project is licensed under the MIT License.
Copyright © 2024 Manuel Lõhmus
Donations are welcome and will go towards further development of this project.