Skip to content
tech_e edited this page Dec 23, 2025 · 4 revisions

Bindable Data Sources

Overview

Bindable objects facilitate data encapsulation and reactive update dissemination upon data mutations. Diverse data source types can be defined and extended for enhanced flexibility within applications.

Setting Up Data

Initialization of data sources is performed with an object encapsulating the desired data attributes.

@import { Data } from '@base-framework/base';

const data = new Data({
    name: 'name',
    age: 21,

    // supports deep data
    team: [
        { name: 'name', age: 21},
        // ...
    ]
});

Using Data

Data manipulation within these sources is conducted via getter and setter methods or using the dot notation as the data sources use a data proxy layer to notify the layout when they are updated.

// Retrieval
let prop = data.name;
// or
let name = data.get('name');

// Assignment
data.name = 'batman';
// or
data.set('name', 'batman');

// Batch updates using an object
data.set({
    name: 'batman',
    age: 21
});

// Deletion
data.delete('name');

Event Subscriptions

Subscribe to data changes programmatically using on() and off() methods:

// Subscribe to changes
const token = data.on('name', (newValue, committer) => {
    console.log('Name changed to:', newValue);
});

// Unsubscribe from changes
data.off('name', token);

// Multiple subscriptions
const countToken = data.on('count', (value) => {
    console.log('Count is now:', value);
});

const ageToken = data.on('age', (value) => {
    console.log('Age changed to:', value);
});

// Clean up subscriptions
data.off('count', countToken);
data.off('age', ageToken);

The on() method returns a subscription token that must be used with off() to unsubscribe.

Basic Helper Methods

The BasicData class, from which all data sources inherit, provides utility methods for routine tasks.

Increment

Incrementation is performed on the specified attribute.

data.increment('count'); // adds 1
// or specify amount
data.increment('count', 5); // adds 5

Decrement

Decremental adjustments follow a similar pattern.

data.decrement('count'); // subtracts 1
// or specify amount
data.decrement('count', 3); // subtracts 3

Toggle

Boolean values are toggled using:

data.toggle('selected'); // switches between true/false

Concat

Concatenate values to string or array properties:

// String concatenation
data.set('message', 'Hello');
data.concat('message', ' World'); // Results in 'Hello World'

// Array concatenation (see Deep Data section for array-specific concat)

If Null

Set a value only if the current value is null:

data.set('name', null);
data.ifNull('name', 'Default Name'); // Sets 'Default Name'

data.set('name', 'John');
data.ifNull('name', 'Default Name'); // No change, remains 'John'

Local Storage

Data persistence is achieved via local storage, keyed by a specified identifier.

const STORAGE_KEY = 'key-name';
data.setKey(STORAGE_KEY);

Getting Local Data

Resumption of data is conditional upon prior existence, with defaults provided as necessary.

data.resume(defaultValue);

Storing Local Data

Local storage is updated with the current state of the data object.

data.store();

Generic Shallow Data

SimpleData is optimized for flat data structures, maintaining key-value pairs.

const data = new SimpleData({
    name: 'name',
    age: 21
});

Generic Deep Data

Data objects manage complex, deeply nested structures, with dual-layered staging to facilitate binding and publishing updates, while maintaining a definitive source layer.

const data = new Data({
    team: [
        { name: 'name', age: 21},
        // ...
    ]
});

Data Helper Methods

Nested data manipulation is streamlined with a suite of helper methods.

Splice

Array manipulation is performed relative to the specified index. This removes an item at the given index.

data.splice('team', 0); // Removes first item
data.splice('team', 2); // Removes item at index 2

Push

Appending to arrays is straightforward. Adds an item to the end of the array.

data.push('team', { name: 'name', age: 21});

Concat

Concatenate arrays with multiple values at once:

data.concat('team', [
    { name: 'Alice', age: 25},
    { name: 'Bob', age: 28}
]);

Unshift

Prepends an element to the beginning of an array:

data.unshift('team', { name: 'name', age: 21});

Shift

Removes and returns the first element from an array:

const firstItem = data.shift('team'); // Returns and removes first item

Pop

Removes and returns the last element from an array:

const lastItem = data.pop('team'); // Returns and removes last item

Get Index

Find the index of an item in an array:

// For primitive arrays
data.set('numbers', [1, 2, 3, 4]);
const index = data.getIndex('numbers', 3); // Returns 2

// For object arrays - search by key-value pair
data.set('team', [
    { name: 'Alice', age: 25},
    { name: 'Bob', age: 28}
]);
const bobIndex = data.getIndex('team', 'name', 'Bob'); // Returns 1

Refresh

Value refreshment is enabled for reactive updates.

data.refresh('team');

Revert

Reversion to the previous state is possible prior to committing changes.

data.revert();

Data Scoping

Scoping facilitates localized path referencing within deeply nested data, streamlining binding and manipulation.

// Scoped component data
data.scope('teams[0]');

Models with Services

Model extensions accommodate deep nesting and integrate with API services for data synchronization, supporting default values and custom methods.

const ClientModel = Model.extend({
    // ...
});

Data Linking

Two-way data synchronization is established between bindable objects.

Property Linking

Individual property linking is as simple as:

data.link(otherData, 'prop');

Link Data

Full object linkage synchronizes all attributes.

data.link(otherData);

Clone this wiki locally