PREF
User preferences stored in the browser.
On this page
API
method
PREF.get(key, [expire])
Reads a stored value.
PREF.get(key, [expire])| Parameter | Type | Description |
|---|---|---|
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.
PREF.set('theme', 'dark');
console.log(PREF.get('theme')); // dark
console.log(PREF.theme); // dark
method
PREF.set(key, [value], [expire])
Stores a value.
PREF.set(key, [value], [expire])| Parameter | Type | Description |
|---|---|---|
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.
WATCH('PREF.theme', function(path, value) {
console.log(value); // dark
});
PREF.set('theme', 'dark', '1 day');
PREF.set('theme', 'dark');
PREF.set('theme');
console.log(PREF.theme === undefined); // true
method
PREF.keys()
Lists the stored keys.
PREF.keys()Returns Array The keys.
PREF.set('theme', 'dark');
console.log(PREF.keys()); // ["theme"]
method
PREF.load(callback)
Reads the stored data when the page starts.
PREF.load(callback)| Parameter | Type | Description |
|---|---|---|
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 }.
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.
PREF.save(data)| Parameter | Type | Description |
|---|---|---|
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.
DEF.delaypref = 50;
PREF.save = function(data) {
console.log(data.theme.value); // dark
};
PREF.set('theme', 'dark');