# Leggere, cambiare e osservare lo stato

> Usa SET, GET, UPD, PUSH e WATCH per lavorare con lo stato globale e reagire quando un percorso cambia.

Framework: jcomponent20
Product: jComponent 20 (jcomponent@20)
Language: it
Content-SHA256: 44dc266f80e0c2098f7167a0d96c54e3c2bf00dc7e4c9b4cac673948e8958575
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: jcomponent20@3fedd009a277f50fddc608b97afbec553b67c51b
Review: not-recorded

Lo stato è un unico oggetto globale indirizzato da **percorsi** come `cart.items`. Le funzioni per lavorarci:

- `SET(path, value)` scrive un valore e notifica tutto ciò che è legato al percorso
- `GET(path)` legge un valore
- `UPD(path)` notifica il percorso dopo che hai modificato l'oggetto sul posto (`GET('cart').discount = 5; UPD('cart')`)
- `PUSH(path, value)` aggiunge a un array e notifica; con un `^` prima del percorso aggiunge il valore all'inizio
- `WATCH(path, callback)` esegue il callback con il nuovo valore ogni volta che il percorso cambia; unisci più percorsi con `+`

```html public/index.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>jComponent</title>
	<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
	<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>
	<p><ui-bind path="cart.items" config="text:value ? value.length + ' items' : 'empty'"></ui-bind></p>

	<script>

		var log = [];

		WATCH('cart.items', function(value) {
			log.push(value.length);
		});

		SET('cart.items', []);
		PUSH('cart.items', 'book');
		PUSH('^cart.items', 'pen');

		GET('cart.items').push('lamp');
		UPD('cart.items');

	</script>

</body>
</html>
```
