Spanish Total.js
English
Documentation For AI agents
Contents

PREF

User preferences stored in the browser.

.md GitHub
On this page

PREF stores small values with an expiration in the browser. The values are ready before the first components start. Stored paths can also be read with GET(), as explained in Paths.

API

method

PREF.get(key, [expire])

Reads a stored value.
ParameterTypeDescription
key String The key.
expire? String Stores the value again with this expiration.

Returns Object The value, or undefined when it does not exist or has expired.

A stored value is also available as a property, for example PREF.theme.

Read a value
PREF.set('theme', 'dark');
console.log(PREF.get('theme')); // dark
console.log(PREF.theme); // dark
method

PREF.set(key, [value], [expire])

Stores a value.
ParameterTypeDescription
key String The key.
value? Object The value.
expire? String For example 1 day. Default: 1 month

Returns Object The value.

Without a value, the key is removed. Watchers and components on the path PREF.key are notified. The data is saved after DEF.delaypref milliseconds. The keys get, set, keys, load and save are not available as properties.

Store and watch a value
WATCH('PREF.theme', function(path, value) {
	console.log(value); // dark
});
PREF.set('theme', 'dark', '1 day');
Remove a value
PREF.set('theme', 'dark');
PREF.set('theme');
console.log(PREF.theme === undefined); // true
method

PREF.keys()

Lists the stored keys.

Returns Array The keys.

List the keys
PREF.set('theme', 'dark');
console.log(PREF.keys()); // ["theme"]
method

PREF.load(callback)

Reads the stored data when the page starts.
ParameterTypeDescription
callback Function function(data).

Replace this function to read the data from another place. Pass the data to the callback as an object where each key holds { value, expire }.

Load from your own source
PREF.load = function(callback) {
	callback({ theme: { value: 'dark', expire: NOW.add('1 day') } });
};
setTimeout(function() {
	console.log(PREF.theme); // dark
}, 500);
method

PREF.save(data)

Writes the stored data.
ParameterTypeDescription
data Object The data to save.

Replace this function to save the data to another place. By default, the data goes to the browser storage under DEF.localstorage plus .pref.

Save to your own target
DEF.delaypref = 50;
PREF.save = function(data) {
	console.log(data.theme.value); // dark
};
PREF.set('theme', 'dark');
Made in Europe