databinder-lite
v2.0.1
Published
A lightweight library for data binding.
Readme
DataBinder Class Constructor: constructor() Description: Initializes a new instance of the DataBinder class and sets up the internal state for storing bindings.
Parameters:
None. Returns:
A new instance of the DataBinder class. _ensureArray(input) Description: Ensures that the input is returned as an array. If the input is already an array, it is returned unchanged. If not, it is wrapped in an array.
Parameters:
input (any): The input value to be checked. Returns:
Array: An array containing the input value. _updateElement(target, value) Description: Updates the content of an HTML element based on its tag name. It handles multiple HTML tags such as DIV, SPAN, INPUT, LI, and P. If the target is of a different type, a warning is logged.
Parameters:
target (HTMLElement): The HTML element to update. value (string): The value to update the target element with. Returns:
None. bindOneWay({ source, target, propName = null }) Description: Creates a one-way binding between a source object and a target element. Any change in the source object automatically updates the target elements.
Parameters:
source (object): The source object to bind from. Must be a non-null object. target (HTMLElement | array): The target element(s) to bind to. This can be a single element or an array of elements. propName (string | null, optional): The property name in the target element to bind to. If not specified, the entire object is bound to the target. Returns:
Proxy: A proxy object for the source to enable tracking of changes. Throws:
TypeError: If the source is not an object or is null, or if any target is not a valid HTML element or object. bindTwoWay(inputs, outputs) Description: Creates a two-way binding between input and output elements. Changes made in one will automatically update the other.
Parameters:
inputs (HTMLElement | array): The input elements to bind. Can be a single element or an array of elements. outputs (HTMLElement | array): The output elements to bind to. Can be a single element or an array of elements. Returns:
None. updateValue(variableName, newValue) Description: Manually updates the value of a bound variable. If the variable is found in the bindings, it updates the value and triggers an update for the associated elements.
Parameters:
variableName (string): The name of the variable to update. newValue (any): The new value to assign to the variable. Returns:
None. trigger(variableName) Description: Manually triggers an update for a bound variable. This forces the associated elements to reflect the current value of the variable.
Parameters:
variableName (string): The name of the variable to trigger an update for. Returns:
None. Example Usage javascript Copier Modifier import DataBinder from 'databinder';
// Create an instance of DataBinder const binder = new DataBinder();
// One-Way Binding const dataState = { value: "Loading..." }; const display = document.getElementById('data-display');
binder.bindOneWay({ source: dataState, target: [display] });
// Two-Way Binding const usernameInput = document.getElementById('username'); const preview = document.getElementById('preview-username');
binder.bindTwoWay([usernameInput], [preview]);
// Updating a variable binder.updateValue('username', 'JohnDoe');
// Manually triggering an update binder.trigger('username');
