Spanish Total.js
English
Documentation For AI agents
Contents

Instance reference

The properties, methods and events of the instance object that exports.make receives.

.md GitHub
On this page

The instance is one copy of the component in a design. exports.make receives it as its first argument:

exports.make = function(instance, config, element) {

    instance.on('value', function(value) {
        element.text(value);
    });

};

Entries of type event are names for instance.on(). Entries of type method are functions of the instance; property entries are its data. Some events happen only in the editor, as their entries say.

API

property

instance.id

The id of the instance in the design, for example cid6wh0tx52.
property

instance.config

The configuration of the instance.
property

instance.element

The element of the instance, a jQuery object.

It has the classes UI_component, ui_ and the component id, and the class of the component.

property

instance.dom

The element of the instance, as a DOM element.
property

instance.component

The declaration of the component: exports with id, name, html, readme and the other properties.
property

instance.app

The app of the design. See App reference.
property

instance.parent

The instance that holds this one.
property

instance.children

The instances directly inside this one.
property

instance.args

The Dynamic arguments of the design.
property

instance.state

The state of the instance: value, modified, touched, invalid, disabled and readonly, and the flags bind, notify and validate.

Set instance.state.bind = true to let a Form read and write the value. Set instance.state.notify = true to receive notify instead of new values from the instance you follow. Set instance.state.validate = false to turn off instance.check(). See Values and data binding.

property

instance.cache

An object for your own temporary data.
property

instance.outputs = [{ id, name }]

Outputs of this instance only, instead of exports.outputs.

instance.inputs does the same for the list of inputs in the editor.

delegate

instance.validate = function() {}

Checks the value. Define it in your code.

Returns Boolean true when the value is valid.

Return true when the value is valid, and false or an error text when it is not. instance.check() calls it.

A required number
instance.validate = function() {
	var value = instance.get();
	return value != null && value >= config.min && value <= config.max;
};
delegate

instance.default = function() {}

Returns the default value. Define it in your code.

Returns Object The default value.

A Form sets it when it clears its components.

method

instance.set(type, value, [kind])

Sets the value or another part of the state.
ParameterTypeDescription
type String value, modified, touched, invalid, disabled or readonly.
value Object The new value.
kind? String A word of your own, passed to the set and value events. force sets the value even when it equals the current one; noemitstate does not announce the change to the parents.

Returns Boolean false when nothing changed.

Setting value emits value, passes the value to the instances that follow this one, and runs instance.check(). Setting modified, touched, invalid, disabled or readonly toggles the matching class on the element, for example UI_touched. The change is announced to the parent instances as state.

Set the value after a click
instance.set('value', value);
instance.set('modified', true);
instance.set('touched', true);
method

instance.get([type])

Returns the value or another part of the state.
ParameterTypeDescription
type? String A key of the state. Default: value

Returns Object The value.

method

instance.on(name, fn)

Registers an event handler.
ParameterTypeDescription
name String The name of the event.
fn Function The handler.

Several names can be joined with a space or +.

method

instance.off(name, [fn])

Removes an event handler, or all handlers of the event.
ParameterTypeDescription
name String The name of the event.
fn? Function The handler to remove.
method

instance.emit(name, [a], [b], [c], [d], [e])

Calls the handlers of an event of this instance.
ParameterTypeDescription
name String The name of the event.
a? Object Arguments for the handlers, up to five.
Draw the first look
instance.emit('configure');
method

instance.input(id, fn)

Handles an input of the component.
ParameterTypeDescription
id String The id of the input in exports.inputs.
fn Function Receives (data, callback).
method

instance.output(id, [data])

Sends an output, or registers the function that produces its data.
ParameterTypeDescription
id String The id of the output.
data? Object The data, or a function that receives next.

With data, it sends the data at once. With a function, it registers it; a later call without data runs it with next(err, data). The output must be declared.

Send a value
instance.output('change', value);
method

instance.check([force])

Runs instance.validate and sets invalid.
ParameterTypeDescription
force? Boolean Check at once. Default: false

It runs after 50 ms, or at once with force. A disabled or read-only instance is always valid.

method

instance.errors()

Returns the errors of an invalid instance.

Returns Array A list of { error: String }.

When invalid holds a text or a list of texts, they are returned as objects.

method

instance.reset()

Emits reset on the instance.
method

instance.reconfigure(config)

Changes keys of the configuration and emits configure with the previous values.
ParameterTypeDescription
config Object The keys to change.
method

instance.find(id)

Finds another instance of the design.
ParameterTypeDescription
id String The id, @ and the id, or . and a path.

Returns Object The instance, or undefined.

method

instance.readvalue([id])

Returns the value of this instance or of another one.
ParameterTypeDescription
id? String The id of another instance.

Returns Object The value.

method

instance.watch(id, fn)

Runs fn every time the value of another instance changes.
ParameterTypeDescription
id String The id of the instance.
fn Function Receives the value.
method

instance.datasource(id, fn)

Reads a list of items from a data source and calls fn with a copy.
ParameterTypeDescription
id String The data source, usually the value of a field with dirsource:#datasource.
fn Function Receives the array.

With @ and the id of an instance, it reads its value and calls fn again when the value changes. The value must be an array, or an object with items. Code lists and data views come from the app that shows the design.

method

instance.family([index])

Returns all instances inside this one, at every level.
ParameterTypeDescription
index? Number Only the instances in the area with this data-index.

Returns Array The instances.

method

instance.hidden()

Tells whether the element is hidden.

Returns Boolean true when hidden.

method

instance.read(obj, path)

Reads a value from an object by a path with dots.
ParameterTypeDescription
obj Object The object.
path String For example address.city.

Returns Object The value.

Read a nested key
instance.read({ a: { b: 5 } }, 'a.b'); // 5
method

instance.write(obj, path, value)

Writes a value into an object by a path with dots, and creates the objects on the way.
ParameterTypeDescription
obj Object The object.
path String For example address.city.
value Object The value.

Returns Object The object that holds the last key.

method

instance.maketemplate([html])

Compiles a Tangular template and keeps it in a cache.
ParameterTypeDescription
html? String The template.

Returns Function A function that returns HTML for a model.

Without html, it uses the <body> of the component.

Render a label
var label = instance.maketemplate('<label>{{ name }}:</label>');
element.html(label(config));
method

instance.replace(text, [data], [encoder])

Fills {args.key}, {config.key}, {user.key} and {data.key} in a text.
ParameterTypeDescription
text String The text.
data? Object The data for {data.key}.
encoder? String url, html or json to encode the values, or a function.

Returns String The text.

args are the Dynamic arguments, config the configuration, user the object window.user of the page and data the second argument.

Use a dynamic argument
instance.replace('Hi {args.who}'); // 'Hi Jane' when the argument who is Jane
method

instance.wait(fn, callback)

Calls callback when fn returns a true value, checking every 300 ms.
ParameterTypeDescription
fn Function The check.
callback Function Runs once.
method

instance.error(err)

Writes an error to the console with the names of the component and the instance.
ParameterTypeDescription
err Object The error.
method

instance.edit(el, [options])

Makes an element editable on the canvas. Editor only.
ParameterTypeDescription
el Element The element.
options? Object For example { multiline: true }.
method

instance.remove()

Removes the instance from the design.
event

instance.on('configure', function(changes) {})

The configuration has changed. Editor.
ParameterTypeDescription
changes Object The previous values.

Emitted after the user submits the settings form and after instance.reconfigure(). changes holds the previous values of the keys that changed.

event

instance.on('ready', function() {})

All instances of the design are built.

Emitted once when the design is built. An instance that the user drops onto the canvas later does not get it.

event

instance.on('value', function(value, kind) {})

The value has changed.
ParameterTypeDescription
value Object The new value.
kind? String The kind given to instance.set().
event

instance.on('set', function(type, value, kind) {})

A part of the state has changed.
ParameterTypeDescription
type String The key of the state, for example value or touched.
value Object The new value.
kind? String The kind given to instance.set().
event

instance.on('notify', function(value, from) {})

The instance you follow has a new value, with instance.state.notify on.
ParameterTypeDescription
value Object A copy of the value.
from Object The instance that has the value.
event

instance.on('state', function(e) {})

The state of an instance inside this one has changed.
ParameterTypeDescription
e Object The event.

e.instance is that instance, e.changes the keys that changed, for example { value: 1 }, and e.state its state. e.stopPropagation() stops the event at this instance. Other instances receive the same event as @state.

event

instance.on('reset', function() {})

A Form or instance.reset() resets the instance.

Clear touched and modified here.

event

instance.on('resize', function(meta) {})

The window has changed its size.
ParameterTypeDescription
meta Object The size.

meta.width and meta.height are the size of the design, meta.display the display size: xs, sm, md or lg.

event

instance.on('service', function(counter) {})

Emitted every minute while the design is on the page.
ParameterTypeDescription
counter Number How many times it has been emitted.
event

instance.on('destroy', function() {})

The instance is removed. Stop timers here.
event

instance.on('settings', function(config) {})

The settings form is about to open. Editor.
ParameterTypeDescription
config Object The copy.

config is the copy of the configuration that the form edits.

event

instance.on('stringify', function(meta, isbuild) {})

The design is being saved or published. Editor.
ParameterTypeDescription
meta Object The data of the instance.
isbuild Boolean true when publishing.

meta is the saved data of the instance; change meta.config to change what is saved. isbuild is true when the design is published.

event

instance.on('refresh', function(meta) {})

Something in the design has changed. Editor.
ParameterTypeDescription
meta Object What changed.

meta.type is configure (another instance changed its settings, in meta.item), remove (instances were removed, in meta.items), paths or apps.

event

instance.on('html', function(el) {})

The user has edited text, an icon or a link of the instance on the canvas. Editor.
ParameterTypeDescription
el jQuery The edited element.
Made in Europe