Instance reference
The properties, methods and events of the instance object that exports.make receives.
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.
instance.id
property
instance.config
The configuration of the instance.
instance.config
property
instance.element
The element of the instance, a jQuery object.
instance.elementIt 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.
instance.dom
property
instance.component
The declaration of the component: exports with id, name, html, readme and the other properties.
instance.component
property
instance.app
The app of the design. See App reference.
instance.app
property
instance.parent
The instance that holds this one.
instance.parent
property
instance.children
The instances directly inside this one.
instance.children
property
instance.args
The Dynamic arguments of the design.
instance.args
property
instance.state
The state of the instance: value, modified, touched, invalid, disabled and readonly, and the flags bind, notify and validate.
instance.stateSet 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.
instance.cache
property
instance.outputs = [{ id, name }]
Outputs of this instance only, instead of exports.outputs.
instance.outputs = [{ id, name }]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.
instance.validate = function() {}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.
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.
instance.default = function() {}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.
instance.set(type, value, [kind])| Parameter | Type | Description |
|---|---|---|
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.
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.
instance.get([type])| Parameter | Type | Description |
|---|---|---|
type? |
String |
A key of the state. Default: value |
Returns Object The value.
method
instance.on(name, fn)
Registers an event handler.
instance.on(name, fn)| Parameter | Type | Description |
|---|---|---|
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.
instance.off(name, [fn])| Parameter | Type | Description |
|---|---|---|
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.
instance.emit(name, [a], [b], [c], [d], [e])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the event. |
a? |
Object |
Arguments for the handlers, up to five. |
instance.emit('configure');
method
instance.input(id, fn)
Handles an input of the component.
instance.input(id, fn)| Parameter | Type | Description |
|---|---|---|
id |
String |
The id of the input in exports.inputs. |
fn |
Function |
Receives (data, callback). |
See Inputs and outputs.
method
instance.output(id, [data])
Sends an output, or registers the function that produces its data.
instance.output(id, [data])| Parameter | Type | Description |
|---|---|---|
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.
instance.output('change', value);
method
instance.check([force])
Runs instance.validate and sets invalid.
instance.check([force])| Parameter | Type | Description |
|---|---|---|
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.
instance.errors()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.
instance.reset()
method
instance.reconfigure(config)
Changes keys of the configuration and emits configure with the previous values.
instance.reconfigure(config)| Parameter | Type | Description |
|---|---|---|
config |
Object |
The keys to change. |
method
instance.find(id)
Finds another instance of the design.
instance.find(id)| Parameter | Type | Description |
|---|---|---|
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.
instance.readvalue([id])| Parameter | Type | Description |
|---|---|---|
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.
instance.watch(id, fn)| Parameter | Type | Description |
|---|---|---|
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.
instance.datasource(id, fn)| Parameter | Type | Description |
|---|---|---|
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.
instance.family([index])| Parameter | Type | Description |
|---|---|---|
index? |
Number |
Only the instances in the area with this data-index. |
Returns Array The instances.
method
instance.read(obj, path)
Reads a value from an object by a path with dots.
instance.read(obj, path)| Parameter | Type | Description |
|---|---|---|
obj |
Object |
The object. |
path |
String |
For example address.city. |
Returns Object The value.
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.
instance.write(obj, path, value)| Parameter | Type | Description |
|---|---|---|
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.
instance.maketemplate([html])| Parameter | Type | Description |
|---|---|---|
html? |
String |
The template. |
Returns Function A function that returns HTML for a model.
Without html, it uses the <body> of the component.
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.
instance.replace(text, [data], [encoder])| Parameter | Type | Description |
|---|---|---|
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.
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.
instance.wait(fn, callback)| Parameter | Type | Description |
|---|---|---|
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.
instance.error(err)| Parameter | Type | Description |
|---|---|---|
err |
Object |
The error. |
method
instance.edit(el, [options])
Makes an element editable on the canvas. Editor only.
instance.edit(el, [options])| Parameter | Type | Description |
|---|---|---|
el |
Element |
The element. |
options? |
Object |
For example { multiline: true }. |
method
instance.remove()
Removes the instance from the design.
instance.remove()
event
instance.on('configure', function(changes) {})
The configuration has changed. Editor.
instance.on('configure', function(changes) {})| Parameter | Type | Description |
|---|---|---|
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.
instance.on('ready', function() {})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.
instance.on('value', function(value, kind) {})| Parameter | Type | Description |
|---|---|---|
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.
instance.on('set', function(type, value, kind) {})| Parameter | Type | Description |
|---|---|---|
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.
instance.on('notify', function(value, from) {})| Parameter | Type | Description |
|---|---|---|
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.
instance.on('state', function(e) {})| Parameter | Type | Description |
|---|---|---|
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.
instance.on('reset', function() {})Clear touched and modified here.
event
instance.on('resize', function(meta) {})
The window has changed its size.
instance.on('resize', function(meta) {})| Parameter | Type | Description |
|---|---|---|
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.
instance.on('service', function(counter) {})| Parameter | Type | Description |
|---|---|---|
counter |
Number |
How many times it has been emitted. |
event
instance.on('destroy', function() {})
The instance is removed. Stop timers here.
instance.on('destroy', function() {})
event
instance.on('settings', function(config) {})
The settings form is about to open. Editor.
instance.on('settings', function(config) {})| Parameter | Type | Description |
|---|---|---|
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.
instance.on('stringify', function(meta, isbuild) {})| Parameter | Type | Description |
|---|---|---|
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.
instance.on('refresh', function(meta) {})| Parameter | Type | Description |
|---|---|---|
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.
instance.on('html', function(el) {})| Parameter | Type | Description |
|---|---|---|
el |
jQuery |
The edited element. |