ForgejComponent
en

jComponent

Data helpers: QUERIFY, STRINGIFY, ENCRYPT, FN, CL, TRANSLATE, UNAUTHORIZED and Thelpers

jComponent 20 · jcomponent@20 jcomponentfrontendutilitiestemplates Executed with jComponent 20
  • QUERIFY([url], object) makes a query string: null values are skipped, arrays are joined with commas, dates become ISO texts and everything is encoded. With a url it is added to it (? or &), also after a method as in 'POST /api/x'.
  • STRINGIFY(object, [compress], [fields]) is JSON.stringify() with two options: compress: true leaves out empty texts, null and false, and trims the texts; fields is a list of the names that are kept, a function (key, value) that decides, or an object where name: false leaves a field out.
  • ENCRYPT(value, [key], [type]) turns a text or an object into a hexadecimal text that ends with a check of the key, and DECRYPT(text, [key], [type]) gives the value back, or undefined when the key or the type are not the same. It is an obfuscation for the browser, not security: anybody can read it without the key.
  • FN('n => n * 2') compiles an expression into a function and remembers it; without => the text is the body and the argument is called value.
  • NOOP, EMPTYARRAY and EMPTYOBJECT (frozen: they cannot be changed by mistake), MONTHS and DAYS (the names in English) and NOW (a Date that jComponent keeps up to date).
  • Code lists: CLINIT(name, function(next) {}, [expire], [init]) registers a list that function loads (it calls next(value); with a text instead of a function, an address like GET /api/countries/). CL('a,b', callback) loads the lists that are not loaded and stores each one in DEF.cl.name (and in the state at that path); CLRELOAD(name, callback) loads them again.
  • Translation: LOCALIZE(text) loads a dictionary in the format of the Total.js resource files (key : translation, one by line, // for comments) and TRANSLATE(text) replaces every @(text) with its translation (the same text when there is none). The key is T plus the HASH() of the text in base 36.
  • UNAUTHORIZED(...names) returns false when window.user has one of the permissions (user.permissions or user.roles, a list or an object) or is sa / su, and true otherwise (also when there is no user).
  • Thelpers (the same object as Tangular.helpers, and Ta is Tangular) holds the helpers of the templates: add one with Thelpers.name = function(value, ...arguments), where this is the model, and use it as {{ value | name(argument) }}.
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 class="out"></p>
</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