Call code with commands and EXEC
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 withinstance.CMD(name, a, b, c)(element[0].$uicomponentis the instance of aui-component). Use it for actions such as "open this window" or "refresh that list". The globalCMD(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, orEXEC(). EXEC('plugin/method', a, b)calls the methodmethodof the plugin that lives on the pathplugin; a plain function name (EXEC('myfunction')) calls a global function. Theexeccommand ofui-bindand theexecoption of many components take the same names.
<!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