---
title: Instance reference
project: UI Builder
version: 1.0
section: development
language: en
license: MIT
url: https://totaljs.es/docs/uibuilder/1-0/development/instance-api/
updated: 2026-11-20T06:51:57.969Z
---

# Instance reference

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

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

```js
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

### instance.id

Type: `property`

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

### instance.config

Type: `property`

The configuration of the instance.

### instance.element

Type: `property`

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.

### instance.dom

Type: `property`

The element of the instance, as a DOM element.

### instance.component

Type: `property`

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

### instance.app

Type: `property`

The app of the design. See [App reference](../app-api/).

### instance.parent

Type: `property`

The instance that holds this one.

### instance.children

Type: `property`

The instances directly inside this one.

### instance.args

Type: `property`

The **Dynamic arguments** of the design.

### instance.state

Type: `property`

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](../values-and-binding/).

### instance.cache

Type: `property`

An object for your own temporary data.

### instance.outputs = [{ id, name }]

Type: `property`

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

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

### instance.validate = function() {}

Type: `delegate`

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.

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

### instance.default = function() {}

Type: `delegate`

Returns the default value. Define it in your code.

Returns: `Object` The default value.

A **Form** sets it when it clears its components.

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

Type: `method`

Sets the value or another part of the state.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | `value`, `modified`, `touched`, `invalid`, `disabled` or `readonly`. |
| `value` | `Object` | yes |  | The new value. |
| `kind` | `String` | no |  | 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`.

Example: Set the value after a click
```js
instance.set('value', value);
instance.set('modified', true);
instance.set('touched', true);
```

### instance.get([type])

Type: `method`

Returns the value or another part of the state.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | no | `value` | A key of the state. |

Returns: `Object` The value.

### instance.on(name, fn)

Type: `method`

Registers an event handler.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the event. |
| `fn` | `Function` | yes |  | The handler. |

Several names can be joined with a space or `+`.

### instance.off(name, [fn])

Type: `method`

Removes an event handler, or all handlers of the event.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the event. |
| `fn` | `Function` | no |  | The handler to remove. |

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

Type: `method`

Calls the handlers of an event of this instance.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the event. |
| `a` | `Object` | no |  | Arguments for the handlers, up to five. |

Example: Draw the first look
```js
instance.emit('configure');
```

### instance.input(id, fn)

Type: `method`

Handles an input of the component.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | yes |  | The id of the input in `exports.inputs`. |
| `fn` | `Function` | yes |  | Receives `(data, callback)`. |

See [Inputs and outputs](../inputs-outputs/).

### instance.output(id, [data])

Type: `method`

Sends an output, or registers the function that produces its data.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | yes |  | The id of the output. |
| `data` | `Object` | no |  | 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.

Example: Send a value
```js
instance.output('change', value);
```

### instance.check([force])

Type: `method`

Runs `instance.validate` and sets `invalid`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `force` | `Boolean` | no | `false` | Check at once. |

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

### instance.errors()

Type: `method`

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.

### instance.reset()

Type: `method`

Emits `reset` on the instance.

### instance.reconfigure(config)

Type: `method`

Changes keys of the configuration and emits `configure` with the previous values.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `config` | `Object` | yes |  | The keys to change. |

### instance.find(id)

Type: `method`

Finds another instance of the design.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | yes |  | The id, `@` and the id, or `.` and a path. |

Returns: `Object` The instance, or `undefined`.

### instance.readvalue([id])

Type: `method`

Returns the value of this instance or of another one.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | no |  | The id of another instance. |

Returns: `Object` The value.

### instance.watch(id, fn)

Type: `method`

Runs `fn` every time the value of another instance changes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | yes |  | The id of the instance. |
| `fn` | `Function` | yes |  | Receives the value. |

### instance.datasource(id, fn)

Type: `method`

Reads a list of items from a data source and calls `fn` with a copy.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `id` | `String` | yes |  | The data source, usually the value of a field with `dirsource:#datasource`. |
| `fn` | `Function` | yes |  | 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.

### instance.family([index])

Type: `method`

Returns all instances inside this one, at every level.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `index` | `Number` | no |  | Only the instances in the area with this `data-index`. |

Returns: `Array` The instances.

### instance.hidden()

Type: `method`

Tells whether the element is hidden.

Returns: `Boolean` `true` when hidden.

### instance.read(obj, path)

Type: `method`

Reads a value from an object by a path with dots.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `obj` | `Object` | yes |  | The object. |
| `path` | `String` | yes |  | For example `address.city`. |

Returns: `Object` The value.

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

### instance.write(obj, path, value)

Type: `method`

Writes a value into an object by a path with dots, and creates the objects on the way.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `obj` | `Object` | yes |  | The object. |
| `path` | `String` | yes |  | For example `address.city`. |
| `value` | `Object` | yes |  | The value. |

Returns: `Object` The object that holds the last key.

### instance.maketemplate([html])

Type: `method`

Compiles a Tangular template and keeps it in a cache.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | no |  | The template. |

Returns: `Function` A function that returns HTML for a model.

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

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

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

Type: `method`

Fills `{args.key}`, `{config.key}`, `{user.key}` and `{data.key}` in a text.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `text` | `String` | yes |  | The text. |
| `data` | `Object` | no |  | The data for `{data.key}`. |
| `encoder` | `String` | no |  | `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.

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

### instance.wait(fn, callback)

Type: `method`

Calls `callback` when `fn` returns a true value, checking every 300 ms.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | The check. |
| `callback` | `Function` | yes |  | Runs once. |

### instance.error(err)

Type: `method`

Writes an error to the console with the names of the component and the instance.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `err` | `Object` | yes |  | The error. |

### instance.edit(el, [options])

Type: `method`

Makes an element editable on the canvas. Editor only.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `el` | `Element` | yes |  | The element. |
| `options` | `Object` | no |  | For example `{ multiline: true }`. |

See [Edit text on the canvas](../canvas-editing/).

### instance.remove()

Type: `method`

Removes the instance from the design.

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

Type: `event`

The configuration has changed. Editor.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `changes` | `Object` | yes |  | 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.

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

Type: `event`

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.

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

Type: `event`

The value has changed.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The new value. |
| `kind` | `String` | no |  | The `kind` given to `instance.set()`. |

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

Type: `event`

A part of the state has changed.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | The key of the state, for example `value` or `touched`. |
| `value` | `Object` | yes |  | The new value. |
| `kind` | `String` | no |  | The `kind` given to `instance.set()`. |

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

Type: `event`

The instance you follow has a new value, with `instance.state.notify` on.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | A copy of the value. |
| `from` | `Object` | yes |  | The instance that has the value. |

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

Type: `event`

The state of an instance inside this one has changed.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `e` | `Object` | yes |  | 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`.

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

Type: `event`

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

Clear `touched` and `modified` here.

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

Type: `event`

The window has changed its size.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `meta` | `Object` | yes |  | The size. |

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

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

Type: `event`

Emitted every minute while the design is on the page.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `counter` | `Number` | yes |  | How many times it has been emitted. |

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

Type: `event`

The instance is removed. Stop timers here.

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

Type: `event`

The settings form is about to open. Editor.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `config` | `Object` | yes |  | The copy. |

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

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

Type: `event`

The design is being saved or published. Editor.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `meta` | `Object` | yes |  | The data of the instance. |
| `isbuild` | `Boolean` | yes |  | `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.

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

Type: `event`

Something in the design has changed. Editor.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `meta` | `Object` | yes |  | What changed. |

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

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

Type: `event`

The user has edited text, an icon or a link of the instance on the canvas. Editor.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `el` | `jQuery` | yes |  | The edited element. |

See [Edit text on the canvas](../canvas-editing/).
