Migrating from jComponent 19
Replace every removed or changed jComponent 19 API with its jComponent 20 equivalent, including the changes that fail without an error.
On this page
This guide moves an application from jComponent 19 to jComponent 20.015. It shows the replacement for each function, attribute and setting that was removed or changed, and it marks the changes that give no error in the browser console, because those are the ones a quick test misses.
Update the CDN files
Replace both files. The bundle still contains jQuery, Tangular and the routing functions, so nothing else has to be loaded.
| jComponent 19 | jComponent 20 |
|---|---|
https://cdn.componentator.com/spa.min@19.js | https://cdn.componentator.com/spa.min@20.js |
https://cdn.componentator.com/spa.min@19.css | https://cdn.componentator.com/spa.min@20.css |
Recommended order of work
- Replace the CDN files and open the application with the browser console visible.
- Change the markup first:
data---,data-bindanddata-scopeelements are ignored, so nothing else can be tested until they are converted. - Search the code for the patterns in Changes that fail silently and fix them. The console does not point to them.
- Fix the errors the console reports, such as
VALID is not definedor$(...).scope is not a function. - Port the forms, then test every form by hand: empty, invalid, valid and after a reset.
- Test the screens that load data, because AJAX callbacks receive fewer arguments.
Changes that fail silently
Each of these runs without an error in 20.015 and does the wrong thing. Search your code for the left column.
| jComponent 19 | jComponent 20 |
|---|---|
WATCH('user.name', function(path, value, type) { | WATCH('user.name', function(value, flags, path) { |
WATCH('user.*', function(path) { | WATCH('user', function(value, flags, path) { |
RESET('form.*'); | RESET('form'); |
GET('form @modified') | COMPONENTS('form @modified') |
return value; // a non-empty string means valid | return !!value; |
self.setter = function(value, path, type) { | self.setter = function(value, path, flags) { |
SET('user.name', 'Ann', 100); | setTimeout(() => SET('user.name', 'Ann'), 100); |
SET('form.city', 'Vienna', true); | SET('form.city @reset', 'Vienna'); |
PUSH('cart.items', [2, 3]); | [2, 3].forEach(item => PUSH('cart.items', item)); |
SETTER('box', 'show', 1); | SETTER('box/show', 1); |
FIND('box', function(com) { | WAIT(() => FIND('box'), function() { |
<div data---="box__user.name__size:2"></div> | <ui-component name="box" path="user.name" config="size:2"></ui-component> |
<ui-plugin path="address"> | <ui-plugin path="?.address"> |
config="text;format:2" | config="text:value.format(2)" |
AJAX('GET /orders/', function(response, err) { | ON('response', opt => status = opt.status); |
A PLUGIN() declaration without a <ui-plugin> element also stops running. See Scopes and plugins.
Forms and validation
VALID(), CAN(), DISABLED(), INVALID(), CHANGE(), MODIFIED() and DEFAULT() were removed. Read the state of a form with COMPONENTS() and ERRORS(), and change it with flags. Forms and validation describes the new way of working.
| jComponent 19 | jComponent 20 |
|---|---|
VALID('form.*') | COMPONENTS('form @invalid').length === 0 |
CAN('form.*') | can('form') (the function below) |
DISABLED('form.*') | !can('form') |
ERRORS('form.*') | ERRORS('form @touched', list => console.log(list.length)) |
CHANGE('form.*'); | UPD('form @change'); |
CHANGE('form.*', false); | RESET('form'); |
MODIFIED('form.*') | COMPONENTS('form @modified').map(com => com.path.toString()) |
DEFAULT('filter.*'); | SET('filter @default', {}); |
INVALID('form.name'); | COMPONENTS('form.name').forEach(com => com.reconfigure({ touched: 1, invalid: true })); |
RESET('form.*'); | RESET('form'); |
CAN() has no direct replacement, so keep a small function in your application:
function can(path) {
return COMPONENTS(path + ' @invalid').length === 0 && COMPONENTS(path + ' @touched').length > 0;
}ERRORS() takes a callback. Without one it throws r is not a function. In 19 it returned the fields that were touched and invalid. In 20 it passes every invalid field, so add the @touched flag to keep the old result:
ERRORS('form @touched', list => console.log(list.length)); // 0
SET('form.name @change', '');
ERRORS('form @touched', list => console.log(list.map(com => com.path.toString()))); // ["form.name"]Paths no longer take .*. It does not match anything, and no error is shown. ERRORS() then reports a broken form as valid:
ERRORS('form.*', list => console.log(list.length)); // 0
ERRORS('form', list => console.log(list.length)); // 1GET('form @modified') returns the whole model in 20. Collect the changed fields yourself:
SET('form.city @change', 'Prague');
var changed = {};
COMPONENTS('form @modified').forEach(com => changed[com.path.toString().split('.').pop()] = com.get());
console.log(changed); // {"city":"Prague"}The result of self.validate is read differently. Any truthy value was valid in 19. In 20 a non-empty string is an error message, so a validator that returns the value marks a filled field as invalid. A number such as value.length is invalid too. Return a boolean:
self.validate = function(value) {
return !!value;
};self.validate = function(value) {
return value.length > 0;
};self.validate also lost its second argument, init. Keep your own flag when the first run needs different treatment:
COMPONENT('field', function(self, config) {
var init = true;
self.validate = function(value) {
console.log(init);
init = false;
return true;
};
});Binding an input
The data-jc-bind attribute is ignored, so the input does not write to the model. Call self.autobind20() once the HTML exists:
COMPONENT('textbox', function(self) {
self.make = function() {
self.html('<input type="text" />');
self.autobind20();
};
});The global FORMATTER() and PARSER() were removed. Register them on the component:
self.formatter(function(path, value) {
return value ? value.toUpperCase() : value;
});self.parser(function(path, value) {
return value ? value.toLowerCase() : value;
});Watchers and paths
The arguments of a watcher changed order, and the code keeps running with the wrong values. Paths describes watchers in 20.
| jComponent 19 | jComponent 20 |
|---|---|
WATCH('user.name', function(path, value, type) { | WATCH('user.name', function(value, flags, path) { |
self.watch('user.name', function(path, value, type) { | self.watch('user.name', function(value, flags, path) { |
exports.watch('count', function(value, path, type) { | exports.watch('count', function(value, flags, path) { |
WATCH('user.*', function(path) { | WATCH('user', function(value, flags, path) { |
WATCHONCE('user.name', function(path, value) { | UNWATCH('user.name', once); inside the callback |
ON('watch', 'user.name', function(path, value) { | WATCH('user.name', function(value) { |
A watcher on a parent path also runs for the paths under it, so WATCH('user', ...) replaces the wildcard.
Writing to the model
The third argument of SET() was removed. A delay or a reset flag passed there is ignored, and the value is written at once.
| jComponent 19 | jComponent 20 |
|---|---|
SET('user.name', 'Ann', 100); | setTimeout(() => SET('user.name', 'Ann'), 100); |
SET('form.city', 'Vienna', true); | SET('form.city @reset', 'Vienna'); |
PUSH('cart.items', [2, 3]); | [2, 3].forEach(item => PUSH('cart.items', item)); |
PUSH('cart.items', 'tea'); on a missing path | SET('cart.items', ['tea']); |
NOTIFY('user.name'); | UPD('user.name'); |
EXT('user', { age: 30 }); | SET('user', Object.assign(GET('user'), { age: 30 })); |
MAKE(function(obj) { | A plain object |
EVALUATE('user.age', 'value >= 18') | FN('value => value >= 18')(GET('user.age')) |
REMOVECACHE('token'); | CACHE('token', null, '1 day'); |
CL_INIT('colors', function(next) { | CLINIT('colors', function(next) { |
PUSH() to a path that does not exist notifies no watcher, so create the array with SET().
The third argument of CACHEPATH() is now a default value, and the values saved by jComponent 19 are not read:
CACHEPATH('filter.q', '1 day', 'all');
console.log(GET('filter.q')); // allCalling components and functions
| jComponent 19 | jComponent 20 |
|---|---|
SETTER('box', 'show', 1); | SETTER('box/show', 1); |
RECONFIGURE('box', 'size:2'); | SETTER('box/reconfigure', 'size:2'); |
RETURN('box/name') | FIND('box').name |
FIND('box', function(com) { | WAIT(() => FIND('box'), function() { |
EXEC(true, 'FUNC.start', 'now'); | EXEC('FUNC.start @important', 'now'); |
AEXEC('FUNC.save', 1) | () => EXEC('FUNC.save', 1) |
ADD('box__user.name', '#panel'); | $('#panel').append('<ui-component name="box" path="user.name"></ui-component>'); |
Markup attributes
The attribute forms are ignored without an error, and the elements stay empty. Use the custom elements described in Using components and Data binding.
| jComponent 19 | jComponent 20 |
|---|---|
<div data---="box__user.name__size:2"></div> | <ui-component name="box" path="user.name" config="size:2"></ui-component> |
<span id="name" data-bind="user.name__text"></span> | <ui-bind id="name" path="user.name" config="text"></ui-bind> |
<div id="box" data-scope="panel"> | <ui-plugin id="box" path="panel"> |
Scopes and plugins
There is no scope object in 20. A <ui-plugin> element is the scope, and $(selector).plugin() returns the instance. An element with ? paths outside a <ui-plugin> is not rendered. Plugins describes the plugin object.
| jComponent 19 | jComponent 20 |
|---|---|
var scope = $('#panel').scope(); | var plugin = $('#panel').plugin(); |
console.log(scope.path); // invoice | console.log(plugin.path.toString()); // invoice |
scope.watch('number', function(path, value) { | plugin.watch('number', function(value, flags, path) { |
<ui-plugin path="address"> nested in a plugin | <ui-plugin path="?.address"> |
<ui-plugin path="!shared"> | <ui-plugin path="shared"> |
<ui-plugin path="login" default="{ user: 'guest' }"> | exports.set('user', 'guest'); in the plugin |
function loginready(path, element) { | function loginready(element) { |
config="text:?/format" | config="text:PLUGINS.contact.format(value)" |
A nested <ui-plugin> no longer extends the path of its parent. Write ? in the path to get the old result, otherwise the plugin reads a global path of the same name:
<ui-plugin path="order">
<ui-plugin path="?.address">
<ui-bind id="city" path="?.city" config="text"></ui-bind>
</ui-plugin>
</ui-plugin>A plugin needs its element. In 19 PLUGIN() created the instance on its own. In 20 the callback runs only when a <ui-plugin> element with that path exists, so a plugin without one never starts:
<ui-plugin path="orders"></ui-plugin>| jComponent 19 | jComponent 20 |
|---|---|
exports.extend('filter', { page: 2 }); | exports.set('filter', Object.assign(exports.get('filter'), { page: 2 })); |
PLUGINS.orders.reset(); | RESET(PLUGINS.orders.path.toString()); |
exports.api('/api/ orders_list', { page: 1 }, function() { | API('/api/', 'orders_list', { page: 1 }, function() { |
console.log(exports.modified); // {"city":"Prague"} | The code below |
exports.modified returns the whole model and clears the state of the components. Collect the changed fields first:
var changed = {};
COMPONENTS(exports.path + ' @modified').forEach(com => changed[com.path.toString().split('.').pop()] = com.get());
RESET(exports.path.toString());
console.log(changed); // {"city":"Prague"}ui-bind commands
Several commands of <ui-bind> were removed, and most of them are skipped without an error. The expression of a command is plain JavaScript, so a call inside it replaces most of them. The ui-bind reference lists the commands of 20.
| jComponent 19 | jComponent 20 |
|---|---|
config="text;format:2" | config="text:value.format(2)" |
config="text;currency:eur" | config="text:value.currency('eur')" |
config="text;default:'none'" | config="text:value == null ? 'none' : value" |
config="text;helper:pluralize('no items','# item','# items','# items')" | config="text:value.pluralize('no items','# item','# items','# items')" |
config="text;selector:b" | config="text" child="b" |
config="checked input" | config="checked" |
config="init:FUNC.start;text" | config="exec:FUNC.start;text" |
config="click:FUNC.open" | <button id="a" class="exec" data-exec="FUNC.open">Open</button> |
config="setter:'box/show',value" | config="exec:FUNC.show" |
config="config:'size:' + value" | <ui-component name="box" config="=size:ui.size"></ui-component> |
The value false renders as empty with the text command. Convert the value to a string:
<ui-bind id="a" path="order.count" config="text:value + ''"></ui-bind>Components
The path of a component is now an object. Comparing it with a string gives false, and string methods such as split() do not exist on it, so convert it first. The third argument of self.setter is an object with flags in place of a number. Creating components describes the delegates.
| jComponent 19 | jComponent 20 |
|---|---|
self.path === 'form.name' | self.path.toString() === 'form.name' |
self.setter = function(value, path, type) { | self.setter = function(value, path, flags) { |
type === 0 | flags.init |
self.notify(); | self.upd(); |
self.faicon('home') | self.icon('home') |
self.scopepath('user.age') | self.makepath('user.age') |
self.inc(2); | self.set(self.get() + 2); |
self.extend({ b: 2 }); | self.set(Object.assign(self.get(), { b: 2 })); |
self.dirty() | !config.touched |
self.dirty(false); | self.touch(); |
self.invalid(); | self.reconfigure({ touched: 1, invalid: true }); |
self.skip(); before self.set(2); | self.rewrite(2); |
self.main() | self.element.parent().component() |
self.exec('show', 1) | self.element.SETTER('*/show', 1) |
self.data('page', 2); | A variable inside the component |
The arguments of self.state(type, what) have different values in 20. Read the state from config:
self.state = function() {
if (config.touched && config.modified && !changed) {
changed = true;
console.log('changed');
}
};self.bindchanges() was removed. Compare the value in the setter:
var last;
self.setter = function(value) {
if (value !== last) {
last = value;
console.log(value);
}
};Macros and virtual bindings
MACRO(), the macro command, VBIND(), VBINDARRAY() and the vbindarray command were removed. Rewrite a macro as a component, and rewrite a virtual binding as a template command with Tangular.
| jComponent 19 | jComponent 20 |
|---|---|
MACRO('clicks', function(self, element) { | COMPONENT('clicks', function(self) { |
config="macro:clicks" | <ui-component id="c" name="clicks" path="counter"></ui-component> |
VBIND('<div><b data-bind=".name__text"></b></div>') | config="template" |
VBINDARRAY('<li data-bind=".name__text"></li>', '#list') | config="template" |
config="vbindarray" | config="template" |
<ui-component id="c" name="clicks" path="counter"></ui-component>COMPONENT('clicks', function(self) {
self.make = function() {
self.event('click', () => self.set((self.get() || 0) + 1));
};
self.setter = function(value) {
self.text('Clicked ' + (value || 0));
};
});A list that used VBINDARRAY() becomes a template:
<ul id="list">
<ui-bind path="rows" config="template">
<script type="text/html">{{ foreach m in value }}<li>{{ m.name }}</li>{{ end }}</script>
</ui-bind>
</ul>The M object
M still exists and is the same object as jComponent and MAIN. M.version, M.components and M.binders work as before. The methods that changed the model were removed or take different arguments, so use the global functions.
| jComponent 19 | jComponent 20 |
|---|---|
M.set('user.name', 'Ann'); | SET('user.name', 'Ann'); |
M.inc('user.age', 2); | INC('user.age', 2); |
M.push('user.tags', 'new'); | PUSH('user.tags', 'new'); |
AJAX and APIs
The callback of AJAX() receives only the response, and AJAX() no longer returns a Promise. The Global functions reference describes AJAX().
| jComponent 19 | jComponent 20 |
|---|---|
AJAX('GET /orders/', function(response, err) { | ON('response', opt => status = opt.status); |
AJAX('GET /orders/').then(function(response) { | new Promise(resolve => AJAX('GET /orders/', resolve)).then(function(response) { |
AJAX('GET /orders/ error', () => console.log('callback')); | AJAX('GET /orders/ ERROR', () => console.log('callback')); |
AJAX('GET /orders/ {"X-Token":"abc"}', function() { | ON('request', opt => opt.headers['X-Token'] = 'abc'); |
AJAXCONFIG('secure', function(options) { | ON('request', function(options) { |
AJAXCACHE('GET /orders/', null, NOOP, '1 minute'); | AJAX('GET /orders/ <1 minute>', NOOP); |
AJAX('GET /orders/', null, 'orders', 100); | setTimeout(() => AJAX('GET /orders/', 'orders'), 100); |
API('/api/ users_save', { name: 'Ann' }, function() { | API('/api/', 'users_save', { name: 'Ann' }, function() { |
DAPI('users_save', { name: 'Ann' }, function() { | TAPI('users_save', { name: 'Ann' }, function() { |
To read the status code of a response, listen to the response event ahead of the request:
var status = 0;
ON('response', opt => status = opt.status);
AJAX('GET /orders/', function(response) {
console.log(response.error, status); // denied 403
});Settings
| jComponent 19 | jComponent 20 |
|---|---|
DEF.validators.email | DEF.regexp.email |
DEF.pathcommon | DEF.path.common |
DEF.pathcl | DEF.path.cl |
DEF.environment | DEF.env |
DEF.languagehtml = 'sk'; | DEF.language = 'sk'; |
These settings no longer exist: ajaxcredentials, ajaxcredentialshtml, ajaxerrors, customelements, delay, delaybinder, delaykeypress, delaypref, delayrepeat, delaywatcher, fallbackcache, importcache, jsoncompress, jsonconverter, jsondate, keypress, makeurl, monitor, prefixcssmacros, repeatfocus, scope, secret and versioncomponents. The DEF reference lists the settings of 20.
Events
| jComponent 19 | jComponent 20 |
|---|---|
ON('destroy', name => console.log('removed', name)); | self.destroy = () => console.log('removed', self.name); |
ON('watch', 'user.name', function(path, value) { | WATCH('user.name', function(value) { |
The exec event is no longer emitted. WW and WH are not set until the library starts, so read them inside ON('ready', ...). The Events reference lists the events of 20.
jQuery helpers
| jComponent 19 | jComponent 20 |
|---|---|
$('#panel').components() | $('#panel').COMPONENTS() |
$('#panel').FIND('box') | $('#panel').COMPONENTS().findItem('name', 'box') |
$('#panel').RECONFIGURE('box', 'size:2'); | $('#panel').SETTER('box/reconfigure', 'size:2'); |
$('#panel').EMIT('hello', 1); | EMIT('hello', 1); |
String, number, date and array helpers
| jComponent 19 | jComponent 20 |
|---|---|
'Hello {name}'.arg({ name: 'Ann' }) | 'Hello {name}'.args({ name: 'Ann' }) |
'{{ name }}'.render({ name: 'Ann' }) | Tangular.render('{{ name }}', { name: 'Ann' }) |
'<b>Ann</b>'.removeTags() | '<b>Ann</b>'.replace(/<[^>]*>/g, '') |
'a=1&b=2'.parseEncoded() | Object.fromEntries(new URLSearchParams('a=1&b=2')) |
new Date(2026, 0, 5).toNumber() | +new Date(2026, 0, 5).format('yyyyMMdd') |
new Date(2026, 0, 5).parseDate() | new Date(2026, 0, 5) |
(5).between({ 1: 'low', 10: 'high' }) | value < 10 ? 'low' : 'high' |
tasks.async(() => console.log('done')); | tasks.wait((task, next) => task(next), () => console.log('done')); |
String.parseConfig() no longer accepts a callback, and it ignores one without an error. Loop over the returned object:
var config = 'size:2;open:true'.parseConfig();
for (var key in config)
console.log(key, config[key]);Functions without a replacement
These global functions were removed, and 20.015 has no direct equivalent.
| jComponent 19 | jComponent 20 |
|---|---|
WAPI, WAPI_INIT, AJAXCACHEREVIEW | No replacement |
DEBUG, STOPDEBUG | No replacement |
QUEUE, REPEAT, WORKFLOW | No replacement |
PLUGINABLE, VERSION, IMPORTCACHE | No replacement |
CLEARCACHE, REWRITE, REWRITE2, NOTMODIFIED, BIND, OPT | No replacement |
A complete form
This form has two required fields and a button that is enabled once the form is valid. Both versions behave the same: the first save reports the two invalid paths, and a save with filled fields returns the model.
Before, in jComponent 19:
<ui-component name="textbox" path="form.name" config="label:Name;required:true"></ui-component>
<ui-component name="textbox" path="form.email" config="label:Email;required:true;email:true"></ui-component>
<button id="save" disabled>Save</button>
<script>
COMPONENT('textbox', function(self, config) {
self.validate = function(value) {
if (!config.required)
return true;
if (!value)
return false;
return config.email ? DEF.validators.email.test(value) : true;
};
self.make = function() {
self.html('<label>' + config.label + '</label><input type="text" data-jc-bind />');
};
});
var form = { name: '', email: '' };
WATCH('form.*', function(path, value, type) {
$('#save').prop('disabled', DISABLED('form.*'));
});
function save() {
CHANGE('form.*');
if (!VALID('form.*')) {
console.log(ERRORS('form.*').map(com => com.path));
return;
}
console.log(GET('form @reset'));
}
</script>After, in jComponent 20:
<ui-component name="textbox" path="form.name" config="label:Name;required:true"></ui-component>
<ui-component name="textbox" path="form.email" config="label:Email;required:true;email:true"></ui-component>
<button id="save" disabled>Save</button>
<script>
COMPONENT('textbox', function(self, config) {
self.validate = function(value) {
if (!config.required)
return true;
if (!value)
return false;
return config.email ? DEF.regexp.email.test(value) : true;
};
self.make = function() {
self.html('<label>' + config.label + '</label><input type="text" />');
self.autobind20();
};
});
var form = { name: '', email: '' };
function can(path) {
return COMPONENTS(path + ' @invalid').length === 0 && COMPONENTS(path + ' @touched').length > 0;
}
WATCH('form', function(value, flags, path) {
$('#save').prop('disabled', !can('form'));
});
function save() {
UPD('form @change');
ERRORS('form', function(list) {
if (list.length) {
console.log(list.map(com => com.path.toString()));
return;
}
console.log(GET('form @reset'));
});
}
</script>