Virtual binder
The properties and methods of the objects created by VBIND() and VBINDARRAY().
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.
binder.elementAdd it to the page yourself.
binder.element.appendTo('body');
console.log(binder.element.attr('class')); // row
property
binder.binders
The binders found in the fragment.
binder.binders
property
binder.value
The current model.
binder.valuebinder.set({ name: 'Peter' });
console.log(binder.value.name); // Peter
method
binder.set([path], value, [skipchecksum])
Replaces the model or changes a field of it.
binder.set([path], value, [skipchecksum])| Parameter | Type | Description |
|---|---|---|
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.
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.
binder.get([path])| Parameter | Type | Description |
|---|---|---|
path? |
String |
The path inside the model. |
Returns Object The value.
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.
binder.upd([path], [skipchecksum])| Parameter | Type | Description |
|---|---|---|
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.
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.
binder.on(name, [selector], callback)| Parameter | Type | Description |
|---|---|---|
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.
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.
binder.remove()Returns Object Always null.
binder.remove();
console.log($('.row').length, binder.element); // 0 null
property
list.items
The binders of a VBINDARRAY() list.
list.itemsEach item is a virtual binder with the additional properties index and vbindarray.
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.
list.set(array) or list.set(index, value)| Parameter | Type | Description |
|---|---|---|
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.
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.
list.get([index])| Parameter | Type | Description |
|---|---|---|
index? |
Number |
The index of the item. |
Returns Object|Array The value.
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.
list.upd([index])| Parameter | Type | Description |
|---|---|---|
index? |
Number |
The index of the item. |
Returns Object The list.
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.
list.remove()items.set([{ name: 'Peter' }]);
items.remove();
console.log($('#list').html() === ''); // true