Paths
How paths address values in the data model, how to read and write them, and who is notified of a change.
On this page
A path is a string that names a place in the data model, such as order.address.city. Components, bindings and your own code all use paths to share data. By default a path starts at the window object, so order.address.city is the same value as window.order.address.city.
Read and write values
SET() writes a value and GET() reads it. Missing objects and arrays on the way are created for you, and reading a path that does not exist returns undefined without an error.
SET('shop.cart.total', 120);
console.log(window.shop); // {"cart":{"total":120}}
console.log(GET('shop.cart.total')); // 120
console.log(GET('shop.order.total')); // undefined
SET('shop.items[1].name', 'Keyboard');
console.log(shop.items); // [null,{"name":"Keyboard"}]Helpers for common changes
These functions change a value and notify everything that depends on it:
SET('cart', { count: 1, items: [], open: false, note: 'gift' });
INC('cart.count'); // 2
INC('cart.count', 3); // 5
PUSH('cart.items', 'tea'); // ["tea"]
EXT('cart', { city: 'Rome' }); // adds cart.city and keeps the other fields
TOGGLE('cart.open'); // true
NUL('cart.note'); // null
console.log(cart);
// {"count":5,"items":["tea"],"open":true,"note":null,"city":"Rome"}
WATCH('cart.count', (path, value) => console.log('changed', value));
cart.count = 10; // no notification
UPD('cart.count'); // changed 10| Function | What it does |
|---|---|
SET(path, value) | Replaces the value. |
INC(path, [value]) | Adds value to a number. The default is 1. |
PUSH(path, value) | Adds an item, or every item of an array, to an array. |
EXT(path, object) | Copies the fields of object into the object at the path. |
TOGGLE(path) | Inverts a boolean. |
NUL(path) | Sets the value to null. |
UPD(path) | Keeps the value and notifies its dependents. |
Assigning to a variable directly, as in cart.count = 10, changes the value silently. Call UPD() afterwards to announce the change.
Delay a write
A number of 10 or more as the third argument of SET() delays the write by that many milliseconds. Calling it again for the same path within the delay replaces the pending write.
SET('delayed.value', 1, 100);
console.log(window.delayed); // undefined
setTimeout(() => console.log(delayed.value), 200); // 1Path prefixes
A prefix sends the path to a dedicated storage object:
| Prefix | Stored in | Use it for |
|---|---|---|
% | jctmp | Temporary values. |
* | common | Values shared across the application. |
# | DEF.cl | Code lists, such as the options of a select box. |
SET('%wizard.step', 2);
console.log(jctmp.wizard.step); // 2
SET('*user', 'Ana');
console.log(common.user); // Ana
SET('#colors', ['red', 'green']);
console.log(DEF.cl.colors); // ["red","green"]
console.log(GET('%wizard.step'), GET('*user'), GET('#colors'));
// 2 Ana ["red","green"]Who is notified of a change
A change at a path notifies its parents and its children. Paths on other branches are left alone.
WATCH('order', (path, value) => console.log('order <-', path));
WATCH('order.address', (path, value) => console.log('address <-', path));
WATCH('order.address.city', (path, value) => console.log('city <-', path));
WATCH('order.note', (path, value) => console.log('note <-', path));
SET('order.address.city', 'Madrid');
// order <- order.address.city
// address <- order.address.city
// city <- order.address.city
SET('order.address', { city: 'Lisbon' });
// order <- order.address
// address <- order.address
// city <- order.addressWriting to order.address.city reaches the watchers of order, order.address and order.address.city. The watcher of order.note stays silent in both writes. The same rule applies to components and to bindings.
Watch a path
WATCH() calls a function when a path changes. The function receives the path that changed, the current value at the watched path and the type of the change. Pass true as the third argument to run it immediately with the current value.
WATCH('stock.count', function(path, value, type) {
console.log(path, value, type);
}, true);
// stock.count undefined 0
SET('stock.count', 5);
// stock.count 5 1| Type | Meaning |
|---|---|
0 | The initial value. |
1 | Changed by code. |
2 | Changed by user input. |
3 | Set to the default value. |
Fixed paths
A ! in front of a watched path ignores changes that happen below it. The watcher still reacts to the path itself and to its parents.
WATCH('order.address', () => console.log('normal'));
WATCH('!order.address', () => console.log('fixed'));
SET('order.address.city', 'Madrid'); // normal
SET('order.address', { city: 'Rome' }); // normal, fixed
SET('order', { address: {} }); // normal, fixedEnvironment values
A name in square brackets is replaced by the value stored with ENV():
ENV('tenant', 'acme');
SET('data.[tenant].plan', 'pro');
console.log(window.data); // {"acme":{"plan":"pro"}}Plugin paths
The form name|property addresses a property of a plugin. To read a value you can also write name/property.
PLUGIN('cart', function(exports) {
exports.total = 10;
});
setTimeout(function() {
console.log(GET('cart|total')); // 10
SET('cart|total', 25);
console.log(PLUGINS.cart.total); // 25
console.log(GET('cart/total')); // 25
}, 200);Paths relative to a scope
Inside a scope, the ? character stands for the path of the scope, so ?.name can point to a different value in each part of the page. Scopes explains how this works.