ForgejComponent
en

jComponent

Call code with commands and EXEC

jComponent 20 · jcomponent@20 jcomponentfrontendcommands Executed with jComponent 20

Two ways to trigger code without a direct reference to the object that owns it:

  • A component registers a command with self.command(name, fn), and the code that holds the component instance runs it with instance.CMD(name, a, b, c) (element[0].$uicomponent is the instance of a ui-component). Use it for actions such as "open this window" or "refresh that list". The global CMD(name) is declared to reach every component, but in jComponent 20.014 it cuts a name that has no space, so it does nothing: use the instance, or EXEC().
  • EXEC('plugin/method', a, b) calls the method method of the plugin that lives on the path plugin; a plain function name (EXEC('myfunction')) calls a global function. The exec command of ui-bind and the exec option of many components take the same names.
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>
	<ui-plugin path="cart">
		<p>Items: <ui-bind path="?.count" config="text:value"></ui-bind></p>
	</ui-plugin>
	<ui-component name="notice" id="notice"></ui-component>

	<script>

		PLUGIN('cart', function(self) {
			self.add = function(n) {
				self.set('count', (self.get('count') || 0) + n);
			};
			self.make = function() {
				self.set('count', 0);
			};
		});

		COMPONENT('notice', function(self) {
			self.make = function() {
				self.html('<i class="text"></i>');
				self.command('say', function(text) {
					self.find('.text').text(text);
				});
			};
		});

		function logout(arg) {
			window.loggedout = arg;
		}

	</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