EasyBuilder X Custom Model JavaScript SDK
An EasyBuilder X Custom Model defines reusable behavior and an interface. A Custom Widget is an instance of that model on a Screen or Window. The model may contain internal Widgets; its JavaScript can coordinate their behavior through the model's custom Properties, Methods, and Events. A model may also have no internal Widgets.
To use JavaScript in EasyBuilder Pro, refer to the JS Action/Object SDK documentation.
When a Custom Widget runs its model script, this refers to the CustomModel interface of that instance. Each instance has its own Property values and internal Widgets. The runtime loads the script for each instance in its View's JavaScript context.
The shared Canvas, MouseArea, Container, JavaScript timers, modules, and other API pages are also documented in this reference.
Define the interface first
In the Custom Model editor, create a Property named temperature and a Property named alarmLimit (both LREAL), a BOOL Property named overLimit, and an Event named ThresholdExceeded with one LREAL parameter. Use each member's Name for JS Access in the script. Apply changes before deploying the model as a Custom Widget. A different instance can bind its Properties to different Variables or Properties.
this.property.overLimit = this.property.temperature > this.property.alarmLimit;
const subscriptionId = this.property.$subscribe(
'temperature',
(newValue, oldValue) => {
const overLimit = newValue > this.property.alarmLimit;
this.property.overLimit = overLimit;
if (overLimit && oldValue <= this.property.alarmLimit) {
this.event.ThresholdExceeded.emit(newValue);
}
}
);
// If this script no longer needs the callback:
// this.property.$unsubscribe(subscriptionId);$subscribe accepts a Property path and receives (newValue, oldValue) when that value changes. It returns an ID for $unsubscribe. For structured Properties, a dotted member path can be monitored. See CustomPropertyAccess.
Package interactions with internal Widgets
Bind an internal Widget's Property to a Custom Model Property so the script and the internal Widget share the same value. To package an operation on an internal Widget, define a custom Method in the model with Actions that target that Widget. The script can invoke that Method by its Name for JS Access:
This reference uses those bindings and Actions for editor-placed internal Widgets; it does not define a direct child-object lookup API.
this.method.ResetDisplay();For outward notifications, define a custom Event and call this.event.ThresholdExceeded.emit(value). An Event handler on the Custom Widget can respond without exposing the internal Widget that caused the event. See CustomMethodAccess, CustomEventAccess, and CustomEventEmitter.
The Custom Model interface tutorial walks through these editor settings and their script. The Custom Widget demo provides a downloadable project and four lessons.
Custom Model script interface
The model script can read and write custom Properties with this.property, subscribe to their changes with this.property.$subscribe, invoke custom Methods with this.method.MethodName(...), and emit custom Events with this.event.EventName.emit(...). It can also use this.widget as the Custom Widget's container.
To exchange Device data, bind a custom Property to an EBX Variable or another Property. The script reads and writes the bound value through this.property.
Method and Event namespaces
this.method and this.event are read-only Proxy namespaces. Their members use the model's Name for JS Access, not the display name. A Method or Event with an empty Name for JS Access has no namespace member: it cannot be looked up, called, or emitted through that empty name, and does not appear in enumeration or JSON.
Select a member with dot or bracket notation. For an Event, call the selected member's emit function; the Event name is not a parameter to emit.
this.method.ResetDisplay();
this.event.ThresholdExceeded.emit(this.property.temperature);
const eventName = 'ThresholdExceeded';
if (eventName in this.event) {
this.event[eventName].emit(this.property.temperature);
}
console.log('ResetDisplay' in this.method); // true if defined
console.log(this.method.Unknown); // undefined
console.log(this.event.Unknown); // undefined
console.log('' in this.method); // false
console.log(this.event['']); // undefinedUse in to check whether a named member exists before invoking it. Accessing an unknown member returns undefined; calling it without checking raises the usual JavaScript TypeError.
Enumerate and inspect members
Object.keys, Reflect.ownKeys, for...in, and object spread expose each defined, nonempty member name once, in definition order. These operations obtain handles without invoking Methods or emitting Events.
console.log(Object.keys(this.method));
console.log(Reflect.ownKeys(this.event));
for (const name in this.event) {
console.log(name);
}
const methods = {...this.method};
const events = {...this.event};Both namespaces have a null prototype, so they have no inherited hasOwnProperty function. Use 'ResetDisplay' in this.method; if an own-property check is specifically needed, use Object.prototype.hasOwnProperty.call(this.method, 'ResetDisplay'). This runtime does not provide Object.hasOwn. An explicitly defined Method or Event named hasOwnProperty is still a custom member.
The namespaces reject assignment, Object.defineProperty, deletion of existing members, prototype changes, and Object.preventExtensions, Object.seal, or Object.freeze. Deleting an unknown name succeeds without changing anything. They provide no $raw access and are not iterable with for...of; Symbol.iterator is undefined.
Keep handles when identity matters
Each lookup creates a fresh Method function or Event emitter object. Save a handle to reuse the same reference:
console.log(this.method.ResetDisplay === this.method.ResetDisplay); // false
console.log(this.event.ThresholdExceeded === this.event.ThresholdExceeded); // false
const reset = this.method.ResetDisplay;
const threshold = this.event.ThresholdExceeded;
const emitThreshold = threshold.emit;
reset();
emitThreshold(this.property.temperature);
console.log(reset.name); // 'ResetDisplay'
console.log(emitThreshold.name); // 'emit'Saved handles remain usable after garbage collection. Each call resolves the Method or Event definition again; if the definition is no longer available, invocation raises ReferenceError.
String conversion and JSON
String concatenation, template literals, String, and console.log('%s', ...) produce the namespace label. Conversion does not call custom Methods or emit Events, including members named toString or valueOf.
console.log('Events: ' + this.event); // Events: [object Event]
console.log(`${this.method}`); // [object Method]
console.log(String(this.event)); // [object Event]
console.log('%s', this.method); // [object Method]
console.log(Object.prototype.toString.call(this.method)); // [object Method]
console.log(Object.prototype.toString.call(this.event)); // [object Event]Symbol.toPrimitive and Symbol.toStringTag support these conversions through lookup. They are not namespace members: in reports false, own-property descriptors are undefined, and they do not appear in enumeration, spread, or JSON. Other symbol lookups return undefined.
JSON normally omits Method functions and represents each Event emitter as {} because its emit function is omitted. For a model with only the ResetDisplay Method and ThresholdExceeded Event:
console.log(JSON.stringify(this.method)); // {}
console.log(JSON.stringify(this.event)); // {"ThresholdExceeded":{}}JavaScript's usual special-name rules still apply. A custom Method named toJSON is invoked by JSON.stringify; a custom Method named then makes the Method namespace a thenable for Promise resolution. The namespace does not invent either member when it is undefined.
For readers moving from EB Pro JS Object
The JavaScript language and shared APIs are available in both environments. These are the interface changes when moving an EB Pro JS Object script into an EBX Custom Model:
| EB Pro JS Object | EBX Custom Model |
|---|---|
this.config supplies values from the Config tab. | Define custom Properties and read or write them through this.property. |
driver and driver.promises provide direct Device address access. | Bind custom Properties to EBX Variables or other Properties; the driver namespaces are unavailable. |
| The script defines the JS Object's behavior. | The script defines the Custom Model's behavior and can coordinate internal Widgets behind a reusable Property / Method / Event interface. |
| No custom Property / Method / Event interface. | Read, write, and monitor custom Properties; invoke custom Methods; emit custom Events. |
this.state and this.playSound() are available. | A Custom Model has no this.state; this.playSound() is currently unavailable. |
window is an alias for the JavaScript global object. | Use globalThis, for example globalThis.memoryStorage; EBX does not provide the window alias. |
A Custom Model with no internal Widgets can cover the behavior-only use case of a JS Object. The EB Pro JsObject page, Config examples, driver pages, and JS Action tutorials are excluded from this EBX reference.