Find and call components and plugins: FIND, COMPONENTS, SETTER, EXE, TAPI, MIDDLEWARE and ADAPT
FIND(selector, [all])returns a component that is ready:'notice'is its name,'.msg'its path (it matches the paths that contain it) and'#id'its id; withallset totrueit returns the list.COMPONENTS(path)returns the list of the ready components whose path is underpath; the flags of a path filter it ('msg @visible').SETTER('name/method', a, b, c, d)callsmethodof every ready component with that name ('.path/method'for a path,'#id/method'for an id,'*/method'for all of them). A name that is not loaded yet is loaded first (a lazy component).ASETTER(...)does not call it: it returns a function that will.EXE(name, a, b, c, d)(the same asEXEC()) calls what the name says:'plugin/method'a method of a plugin,'function'a global function, or a path in the state that holds a function ('FUNC.save').SEEX(name, value)is the same, but when the name is not a function it sets the path tovalue.TAPI(name, data, callback)isAPI(DEF.api, ...)andPIPE(name, data, callback)isAPI(DEF.pipe, ...): two addresses of endpoints that you set once inDEF.apiandDEF.pipe.MIDDLEWARE(name, function(next) {})registers a function that runs before a route: it has to callnext()to let it go on. A route asks for one (or more, separated by commas) as its third argument:ROUTE('/path/', handler, 'name').MIDDLEWARE(['a', 'b'], callback)runs the middleware in order and then the callback; one that does not callnext()stops the chain.NEWUIBIND(element, path, config)makes aui-bindon an element from the code (the element does not need the attributes), and returns it.ADAPT(path, id, text)prepares the text of a component that comes from outside: it translates@(...), replaces~PATH~andCLASSwith the path and~ID~with the id, and puts thepathin the<ui-plugin>tags that do not have one.COMPILEis the same function asNOOPin jComponent 20: it does nothing.- The objects behind everything:
jComponent,Total,MAINandMare the same object (the engine:M.componentsis the list of the components,M.versionthe version),Wiswindow,DEFthe defaults, andREPO,FUNC,TEMPandPLUGINSare plain objects of yours:FUNCfor functions thatEXE('FUNC.name')can call,TEMPis emptied by jComponent when the page starts again.
NEWACTION('Math/add', {
name: 'Add two numbers',
input: '*a:Number, *b:Number',
action: function($, model) {
$.callback({ sum: model.a + model.b });
}
});exports.install = function() {
ROUTE('API /api/ +math_add --> Math/add');
ROUTE('API /pipe/ +math_add --> Math/add');
};<!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-component name="notice" path="msg"></ui-component>
<ui-component name="notice" path="other"></ui-component>
<ui-plugin path="shop"></ui-plugin>
<p id="target">x</p>
<script>
COMPONENT('notice', function(self) {
self.make = function() { self.element.html('<b></b>'); };
self.say = function(text, extra) { self.find('b').text(text + (extra || '')); };
});
PLUGIN('shop', function(exports) {
exports.hello = function(a) { window.shoplog = 'hello ' + a; };
});
window.globalfn = function(a, b) { window.globallog = [a, b]; };
FUNC.local = function(a) { window.funclog = a; };
DEF.api = '/api/';
DEF.pipe = '/pipe/';
</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