Paths
How to address, read, change and watch values of the model with string paths.
On this page
A path is a string that addresses a value in the global window object: app.user.name is the same value as window.app.user.name. All data in jComponent is read and written through paths, which is how the library knows what has changed and who needs to be told.
Reading and writing values
SET(path, value) writes a value and notifies everything that depends on the path. GET(path) reads a value.
SET('app.user.name', 'Ana');
console.log(app.user.name);
console.log(GET('app.user.name'));
console.log(GET('app.user'));
console.log(GET('app.company.name'));Ana
Ana
{"name":"Ana"}
undefinedSET() creates the missing objects along the path. GET() returns undefined when a part of the path does not exist.
The first part of a path is a global variable, so choose a name that the browser does not already use. Names such as name, location or status belong to window.
Path syntax
| Path | Meaning |
|---|---|
app.user.name | A nested key |
shop.products[1].name | A key of an existing array item |
shop.tags[] | With SET(), appends the value to an array and creates the array when it is missing |
shop.prices['pen.blue'] | A key that contains a dot |
SET('shop.products', [{ name: 'Pen' }, { name: 'Book' }]);
SET('shop.products[1].name', 'Notebook');
console.log(GET('shop.products[1].name'));
SET('shop.tags[]', 'new');
SET('shop.tags[]', 'sale');
console.log(GET('shop.tags'));
SET("shop.prices['pen.blue']", 12);
console.log(GET("shop.prices['pen.blue']"));Notebook
["new","sale"]
12Changing values
| Function | What it does |
|---|---|
SET(path, value) | Writes a value |
INC(path, [value]) | Adds a number to the value, 1 by default |
TOGGLE(path) | Negates the value |
PUSH(path, value) | Appends a value to an existing array. With the prefix ^, the value goes to the start of the array |
NUL(path) | Writes null |
SET('cart.count', 1);
INC('cart.count');
INC('cart.count', 5);
console.log(cart.count);
TOGGLE('cart.open');
console.log(cart.open);
SET('cart.items', ['b']);
PUSH('cart.items', 'c');
PUSH('^cart.items', 'a');
console.log(cart.items);
NUL('cart.count');
console.log(cart.count);7
true
["a","b","c"]
nullWho is notified
A change reaches listeners on the same path, on its parent paths and on its child paths. Listeners on sibling paths are left alone.
WATCH('order', () => console.log('order'));
WATCH('order.customer', () => console.log('order.customer'));
WATCH('order.customer.name', () => console.log('order.customer.name'));
WATCH('order.total', () => console.log('order.total'));
SET('order.customer', { name: 'Ana' });order
order.customer
order.customer.nameThe watcher of order.total does not run, because order.customer is neither its parent nor its child.
Changing the model directly
The model is a plain object, so you can change it with normal JavaScript. The library does not see such a change. Call UPD(path) to announce it:
SET('todo.items', ['Write']);
WATCH('todo.items', function(value, flags, path) {
console.log(path, value.length);
});
// Changing the object directly notifies nobody
todo.items.push('Test');
// UPD() tells everybody that the value has changed
UPD('todo.items');todo.items 2Watching a path
WATCH(path, callback, [init]) runs a function whenever the value at the path changes. UNWATCH(path, [callback]) removes the watcher. Without the callback argument, it removes all watchers of the path.
The callback receives three arguments:
| Argument | Description |
|---|---|
value | The current value at the watched path |
flags | An object with the flags of the operation |
path | The path that was changed, which can be a parent or a child of the watched path |
function onchange(value, flags, path) {
console.log(path, value);
}
WATCH('stats.visits', onchange);
SET('stats.visits', 1);
UNWATCH('stats.visits', onchange);
SET('stats.visits', 2);
// Several paths with one callback
WATCH('stats.a + stats.b', onchange);
SET('stats.a', 10);
SET('stats.b', 20);
// The third argument calls the callback right after the registration
SET('stats.c', 30);
WATCH('stats.c', onchange, true);stats.visits 1
stats.a 10
stats.b 20
stats.c 30To display a value on the page, you do not need a watcher. Use data binding.
Shortcuts
Paths support prefixes for common places in the model, such as *title for common.title. They are described in Flags and path modifiers.