Spanish Total.js
English
Documentation For AI agents
Contents

Virtual binder

The properties and methods of the objects created by VBIND() and VBINDARRAY().

.md GitHub
On this page

A virtual binder renders an HTML fragment from its own model, without using the global data model. VBIND() creates one binder, and VBINDARRAY() creates a list with one binder per item. Paths inside the fragment start with a dot. Virtual bindings explains the concept.

API

property

binder.element

The rendered fragment as a jQuery object.

Add it to the page yourself.

Inserting the fragment
binder.element.appendTo('body');
console.log(binder.element.attr('class'));  // row
property

binder.binders

The binders found in the fragment.

See the refresh() and exec() methods.

Counting binders
console.log(binder.binders.length);  // 1
property

binder.value

The current model.
Reading the model
binder.set({ name: 'Peter' });
console.log(binder.value.name);  // Peter
method

binder.set([path], value, [skipchecksum])

Replaces the model or changes a field of it.
ParameterTypeDescription
path? String The path inside the model.
value Object The model or the value of the field.
skipchecksum? Boolean true skips the checksum of an item in VBINDARRAY().

Returns VBinder The binder.

With one argument, the value becomes the whole model. With a path, the model must already exist and the value must not be null.

Using set()
binder.set({ name: 'Peter' });
binder.set('name', 'Anna');
console.log(binder.element.text());  // Anna
method

binder.get([path])

Reads the model or a field of it.
ParameterTypeDescription
path? String The path inside the model.

Returns Object The value.

Using get()
binder.set({ name: 'Peter' });
console.log(binder.get('name'));  // Peter
console.log(binder.get());  // {"name":"Peter"}
method

binder.upd([path], [skipchecksum])

Renders the fragment again.
ParameterTypeDescription
path? String The path inside the model.
skipchecksum? Boolean true skips the checksum of an item in VBINDARRAY().

Returns VBinder The binder.

With a path, only the binders of that path run.

Using upd()
binder.set({ name: 'Peter' });
binder.value.name = 'Anna';
binder.upd('name');
console.log(binder.element.text());  // Anna
method

binder.on(name, [selector], callback)

Attaches a jQuery event to the fragment.
ParameterTypeDescription
name String The name of the event.
selector? String The delegated selector.
callback Function The handler.

Returns VBinder The binder.

The arguments are the same as in the jQuery on() method.

Using on()
binder.on('click', 'ui-bind', () => console.log('clicked'));  // clicked
binder.element.find('ui-bind').click();
method

binder.remove()

Removes the fragment and releases the binder.

Returns Object Always null.

Using remove()
binder.remove();
console.log($('.row').length, binder.element);  // 0 null
property

list.items

The binders of a VBINDARRAY() list.

Each item is a virtual binder with the additional properties index and vbindarray.

Reading the items
items.set([{ name: 'Peter' }, { name: 'Anna' }]);
console.log(items.items.length, items.items[1].index);  // 2 1
method

list.set(array) or list.set(index, value)

Renders an array or changes one item.
ParameterTypeDescription
array Array|Number The array, or the index of the item.
value? Object The new item.

An array renders the list. An index replaces the item at that position. null removes all rendered items.

Using list.set()
items.set([{ name: 'Peter' }, { name: 'Anna' }]);
items.set(1, { name: 'Lucia' });
console.log($('#list').text());  // PeterLucia
method

list.get([index])

Reads the array or one item.
ParameterTypeDescription
index? Number The index of the item.

Returns Object|Array The value.

Using list.get()
items.set([{ name: 'Peter' }]);
console.log(items.get(0).name, items.get().length);  // Peter 1
method

list.upd([index])

Renders the list or one item again.
ParameterTypeDescription
index? Number The index of the item.

Returns Object The list.

Using list.upd()
items.set([{ name: 'Peter' }]);
items.get(0).name = 'Anna';
items.upd(0);
console.log($('#list').text());  // Anna
method

list.remove()

Removes all items and releases the list.
Using list.remove()
items.set([{ name: 'Peter' }]);
items.remove();
console.log($('#list').html() === '');  // true
Made in Europe