Repository navigation
Data
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.
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},
// ...
]
});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');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.
The BasicData class, from which all data sources inherit, provides utility methods for routine tasks.
Incrementation is performed on the specified attribute.
data.increment('count'); // adds 1
// or specify amount
data.increment('count', 5); // adds 5Decremental adjustments follow a similar pattern.
data.decrement('count'); // subtracts 1
// or specify amount
data.decrement('count', 3); // subtracts 3Boolean values are toggled using:
data.toggle('selected'); // switches between true/falseConcatenate 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)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'Data persistence is achieved via local storage, keyed by a specified identifier.
const STORAGE_KEY = 'key-name';
data.setKey(STORAGE_KEY);Resumption of data is conditional upon prior existence, with defaults provided as necessary.
data.resume(defaultValue);Local storage is updated with the current state of the data object.
data.store();SimpleData is optimized for flat data structures, maintaining key-value pairs.
const data = new SimpleData({
name: 'name',
age: 21
});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},
// ...
]
});Nested data manipulation is streamlined with a suite of helper methods.
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 2Appending to arrays is straightforward. Adds an item to the end of the array.
data.push('team', { name: 'name', age: 21});Concatenate arrays with multiple values at once:
data.concat('team', [
{ name: 'Alice', age: 25},
{ name: 'Bob', age: 28}
]);Prepends an element to the beginning of an array:
data.unshift('team', { name: 'name', age: 21});Removes and returns the first element from an array:
const firstItem = data.shift('team'); // Returns and removes first itemRemoves and returns the last element from an array:
const lastItem = data.pop('team'); // Returns and removes last itemFind 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 1Value refreshment is enabled for reactive updates.
data.refresh('team');Reversion to the previous state is possible prior to committing changes.
data.revert();When a parent route or component is configured with persist: true, child
component data is preserved across deactivate/reactivate cycles. On reactivation
the framework merges the persisted stage onto the fresh Data instance
returned by setData() (or by { data: localVar } in a layout).
Persisted non-null values win on changed keys. null/undefined persisted
values are ignored so async refetches can repopulate them. This is correct for
"UI state that should survive navigation" (active tab, scroll position, filter
selection, expanded panels).
If a route is parameterized (e.g. /user/:id) and the persisted child carries
state tied to the previous entity (e.g. isBlocked, displayName), merging
persisted onto fresh will leak User A's state into User B's view.
Opt out per-instance:
setData() {
const data = new Data({ isBlocked: false, displayName: '' });
data.refreshState(); // fresh wins; persisted only fills missing keys
return data;
}For state that should be authoritatively persisted (including null clears):
data.retainState(); // persisted wins for every key, including nullsLayouts using Div({ data: uiState }, [...]) create a temp component whose
setData() returns the closure variable. Each rebuild constructs a fresh
Data, but the framework still merges the persisted prior stage onto it so
e.g. activeFilter: 'document' survives navigation. The merge requires an
active persist chain (persist: true on an ancestor route/component).
Scoping facilitates localized path referencing within deeply nested data, streamlining binding and manipulation.
// Scoped component data
data.scope('teams[0]');Model extensions accommodate deep nesting and integrate with API services for data synchronization, supporting default values and custom methods.
const ClientModel = Model.extend({
// ...
});Two-way data synchronization is established between bindable objects.
Individual property linking is as simple as:
data.link(otherData, 'prop');Full object linkage synchronizes all attributes.
data.link(otherData);