Getting started
Build a working page with bound data and a custom component, starting from an empty HTML file.
On this page
This guide takes you from an empty file to a page with bound data and two custom components. You only need a text editor and a browser.
Load the library
Create a file named index.html and load the script and the stylesheet in the <head>:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My first page</title>
<script src="https://cdn.componentator.com/spa.min@19.js"></script>
<link rel="stylesheet" href="https://cdn.componentator.com/spa.min@19.css" />
</head>
<body>
</body>
</html>Every snippet in the next steps goes inside <body>.
Show a value
Data lives at paths. SET() writes a value, and the <ui-bind> element displays it:
<h1><ui-bind path="page.title" config="text"></ui-bind></h1>
<script>
SET('page.title', 'My first page');
</script>The heading shows My first page. The path attribute says which value to read, and config="text" says to write it as the text of the element. If you run SET('page.title', 'Changed') in the browser console, the heading changes immediately. Data binding lists everything <ui-bind> can do.
Create a component
A component is declared once with COMPONENT() and placed with <ui-component> as many times as you need. This one renders a labelled text box:
<ui-component name="textbox" path="form.name" config="label:Your name"></ui-component>
<script>
COMPONENT('textbox', 'label:Text', function(self, config) {
self.make = function() {
self.html('<label>' + config.label + '</label> <input type="text" data-jc-bind="" />');
};
});
</script>'label:Text'is the default configuration. Theconfigattribute of the element overrides it.self.makeruns once and builds the content of the element.data-jc-bindconnects the input to the path of the component. TypingAnainto the box stores{"name":"Ana"}inform, and writing toform.nameupdates the box.
Add behaviour
Components often need their own logic. The counter component below draws a button and writes to its path through self.set(). Its self.setter function runs each time the value at the path changes, so the screen always follows the data.
<ui-component name="counter" path="form.tickets" config="max:5"></ui-component>
<script>
COMPONENT('counter', 'max:10', function(self, config) {
self.make = function() {
self.html('<button type="button">+1</button> <b></b>');
self.event('click', 'button', function() {
var value = self.get() || 0;
if (value < config.max)
self.set(value + 1);
});
};
self.setter = function(value) {
self.find('b').text(value || 0);
};
});
</script>The complete page
Put everything together. This is the whole file:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My first page</title>
<script src="https://cdn.componentator.com/spa.min@19.js"></script>
<link rel="stylesheet" href="https://cdn.componentator.com/spa.min@19.css" />
</head>
<body>
<h1><ui-bind path="page.title" config="text"></ui-bind></h1>
<ui-component name="textbox" path="form.name" config="label:Your name"></ui-component>
<ui-component name="counter" path="form.tickets" config="max:5"></ui-component>
<p data-bind="form.name__show:value">
<ui-bind path="form" config="text:value.name + ' booked ' + value.tickets + ' ticket(s)'"></ui-bind>
</p>
<script>
COMPONENT('textbox', 'label:Text', function(self, config) {
self.make = function() {
self.html('<label>' + config.label + '</label> <input type="text" data-jc-bind="" />');
};
});
COMPONENT('counter', 'max:10', function(self, config) {
self.make = function() {
self.html('<button type="button">+1</button> <b></b>');
self.event('click', 'button', function() {
var value = self.get() || 0;
if (value < config.max)
self.set(value + 1);
});
};
self.setter = function(value) {
self.find('b').text(value || 0);
};
});
SET('page.title', 'Book your tickets');
SET('form', { name: '', tickets: 1 });
</script>
</body>
</html>Open the file in a browser. The summary line stays hidden until you type a name, because show:value hides the paragraph while form.name is empty. If you type Ana and press the button twice, the page shows Ana booked 3 ticket(s). The counter stops at 5, the value of max in the config attribute.
Run code when the page is ready
The ready event fires once the components on the page are initialized. It needs at least one component on the page.
<ui-component name="clock"></ui-component>
<script>
COMPONENT('clock', function(self) {
self.make = function() {
self.text('12:00');
};
});
ON('ready', function() {
console.log('ready:', FIND('clock').element.text()); // ready: 12:00
});
</script>Next steps
- Learn how values are addressed and who is notified of a change in Paths.
- Connect more of your page with Data binding.
- Give each part of the page its own data with Scopes.
- Go deeper into the component API in Creating components.