State shortcuts: INC, TOGGLE, NUL, UPDATE, UNWATCH, WAIT, BLOCKED and CACHEPATH
These functions work on the state of jComponent by path ('user.visits'), like SET() and GET(), and every one of them notifies the components and the watchers of the path.
INC(path, [value])addsvalue(default1) to a number; when the value is empty it starts from0and when it is a text it is read as a number first.TOGGLE(path)changes a boolean (an empty value becomestrue).NUL(path)and its aliasNULL(path)set the value tonull.UPDATE(path)(aliasUPD) notifies everybody who uses the path without changing the value: use it after you changed an object or an array in place.UNWATCH(path, [fn])removes the watchers of the path made withWATCH(); withfnonly that one.WAIT(fn or path, callback)runscallbackas soon as the function returns something true, or the path has a value. It checks at once and then every 500 ms.BLOCKED(key, timeout, [callback])returnsfalsethe first time andtruefor the nexttimeoutms ('5 seconds'also works): it is a guard against a double click or a repeated request. Thecallbackruns only when it is not blocked.CACHEPATH(path, expire, [default])keeps a value of the state in the cache (CACHE()): it sets the path to the cached value when there is one (or todefault, which can be a function) and saves every change under the keypath_path.- A path is a path in the state of the page, so a name like
nameorlocationis a property ofwindow: use names of your own (person,prefs.mode). setTimeout2(id, fn, ms, [limit], [param])is a debouncedsetTimeout(): calling it again with the sameidrestarts the timer, sofnruns once,msafter the last call. Withlimitthe timer is restarted at most that many times and the following calls are ignored untilfnhas run, so a stream of calls cannot postpone it forever.clearTimeout2(id)cancels it and returnstruewhen there was one.CLONE(value)makes a deep copy (dates stay dates),COPY(from, to)copies the properties of one object into another, deep, and returnsto.NOOPis a function that does nothing andWARN(...)writes toconsole.warn.
<!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 class="out"></p>
<script>
window.sample = { watched: 0 };
</script>
</body>
</html> Try it in your browser
Edit the example and run it. It runs in an isolated frame in your browser; nothing is sent to our server.
Preview