Demo: Custom Widget Properties, Methods, and Events

This demo project shows how a Custom Model script works with the members of its Custom Widget. In four short lessons, the script reads, writes, and monitors custom Properties, controls an internal Widget through an alias Property, invokes custom Methods, and emits custom Events. Each lesson screen pairs a working demo on the left with an excerpt of the model script on the right.
Download the project: EBX-JS-Object-Members-Demo.ebxprj
- The project is made for cMT2078X (V2) (800 x 480) with EasyBuilder X 1.01.
- It uses internal Variables only and needs no Device. Run it with Offline Simulate, open a lesson from the Home screen, and operate the Widgets on the left.
- Each lesson has its own Custom Model, from
JsLesson1_PropertytoJsLesson4_Event. Open a model's Script to read the complete script with its comments. The Variables used by a lesson start withL1_toL4_. - Display text is provided in English and Traditional Chinese. The buttons at the top right of the Home screen switch the language.
| Screen | Custom Model | What it shows |
|---|---|---|
L1_Property | JsLesson1_Property | Read, write, and subscribe to custom Properties |
L2_ObjectProperty | JsLesson2_ObjectProperty | Control an internal Widget through an alias Property |
L3_Method | JsLesson3_Method | Invoke custom Methods whose Actions operate an internal Widget |
L4_Event | JsLesson4_Event | Emit custom Events, and receive taps with a MouseArea |
Lesson 1: Read, write, and subscribe to Properties

The model defines four custom Properties. On the screen, each Property of the Custom Widget is bound to a Variable, and Numeric and Text Widgets show those Variables.
| Property | Data type | Bound to | Role |
|---|---|---|---|
celsius | LREAL | L1_Celsius | Input, set with the slider |
fahrenheit | LREAL | L1_Fahrenheit | Written by the script |
changeCount | DINT | L1_ChangeCount | Written by the script |
message | STRING(64) | L1_Message | Written by the script |
var P = this.property; // property namespace
function toF(c) { return c * 9 / 5 + 32; }
// Read: just like an ordinary field
var c = P.celsius;
// Write: assigning writes it (and its Variable)
P.fahrenheit = toF(c);
// Subscribe: called with (newValue, oldValue)
var id = P.$subscribe('celsius',
function (newValue, oldValue) {
P.fahrenheit = toF(newValue);
P.changeCount = P.changeCount + 1;
P.message = oldValue.toFixed(1) +
' -> ' + newValue.toFixed(1);
});
// When no longer needed: P.$unsubscribe(id);A custom Property is read and written like an ordinary JavaScript field. Writing a Property that is bound to a Variable also writes that Variable.
Drag the slider: L1_Celsius changes, so celsius changes and the $subscribe callback runs with (newValue, oldValue). The callback writes fahrenheit, changeCount, and message, and the Widgets bound to their Variables update. The callback runs for every change of celsius, whatever its source: the slider, a Device, or another script.
Lesson 2: Control an internal Widget through an alias Property

The model contains an internal Rectangle named Box. Its Property boxX was created with Export internal property from the x Property of Box. Such an alias Property has no value of its own: reading or writing this.property.boxX reads or moves Box directly.
| Property | Data type | Bound to | Role |
|---|---|---|---|
boxX | alias of Box.x | - | Position of Box in model coordinates |
running | BOOL | L2_Run | Input, set with the switch |
speed | INT | L2_Speed | Input, pixels per step |
position | INT | L2_Position | Written by the script |
var P = this.property;
var dir = 1, timer = null;
function tick() {
var x = P.boxX + dir * P.speed; // read Box.x
if (x >= 314) { x = 314; dir = -1; }
if (x <= 10) { x = 10; dir = 1; }
P.boxX = x; // write Box.x: Box moves
P.position = x; // shown via a Variable
}
function setRunning(run) {
if (run && !timer) timer = setInterval(tick, 50);
if (!run && timer) {
clearInterval(timer); timer = null;
}
}
P.$subscribe('running', setRunning);
setRunning(P.running);Turn on the switch: running changes, and the $subscribe callback starts a 50 ms setInterval timer. Each tick moves Box by speed pixels and reverses at either end of the model (x from 10 to 314). Turning the switch off clears the timer. The last line applies the current value of running when the script starts.
Lesson 3: Invoke custom Methods

The model contains an internal Switch named Light with three states. The script reaches it through custom Methods, whose Actions call the Switch's own methods:
| Method | Parameters | Action |
|---|---|---|
NextLight | none | Calls increaseState of Light, wrapping from the last state to the first |
SetLight | state (INT) | Calls setState of Light with state |
The stateChanged Event of Light sets the model's light Property to the new state. light is bound to L3_Light, so the screen shows the current state.
| Property | Data type | Bound to | Role |
|---|---|---|---|
running | BOOL | L3_Run | Input, set with the switch |
lastCall | STRING(32) | L3_LastCall | Written by the script |
callCount | DINT | L3_CallCount | Number of NextLight() calls |
light | INT | L3_Light | Current state of Light |
var P = this.property, M = this.method;
var timer = null;
function tick() {
M.NextLight(); // method without arguments
P.lastCall = 'NextLight()';
P.callCount = P.callCount + 1;
}
function setRunning(run) {
if (run && !timer) {
timer = setInterval(tick, 1000);
} else if (!run && timer) {
clearInterval(timer); timer = null;
M.SetLight(0); // method with an argument
P.lastCall = 'SetLight(0)';
}
}
P.$subscribe('running', setRunning);
setRunning(P.running);Turn on the switch: the script calls NextLight() once per second, and turning the switch off calls SetLight(0). Arguments are passed in the order of the Method's parameters. A Method call returns undefined, and the Method's Actions run after the current synchronous script finishes, so a Property that the Actions change still has its old value right after the call.
Lesson 4: Emit and receive Events

| Property | Data type | Bound to | Role |
|---|---|---|---|
value | LREAL | L4_Value | Input, set with the slider |
limit | LREAL | L4_Limit | Input, initially 70 |
| Event | Parameters | Emitted when |
|---|---|---|
OverLimit | value (LREAL) | value rises above limit |
BackToNormal | value (LREAL) | value returns to limit or below |
Tapped | count (DINT) | The Custom Widget is tapped |
var P = this.property, E = this.event;
// Emit: only at the moment value crosses limit
var over = false;
function check() {
var now = P.value > P.limit;
if (now === over) return;
over = now;
if (now) E.OverLimit.emit(P.value);
else E.BackToNormal.emit(P.value);
}
P.$subscribe('value', check);
P.$subscribe('limit', check);
// Receive: JS adds a MouseArea, listens to click
var area = new MouseArea(), taps = 0;
this.widget.add(area); // fills the whole widget
area.on('click', function () {
E.Tapped.emit(++taps); // arguments in defined order
});emit() takes the Event's parameters in their defined order. What happens next is set on the screen, in the Event handlers of the Custom Widget: OverLimit and BackToNormal write the alarm state and value to L4_Alarm and L4_EventValue, and Tapped writes count to L4_TapCount.
The second half receives input. The script creates a MouseArea and adds it to this.widget, the Custom Widget's container, where it fills the whole Widget. Each click emits Tapped. taps is an ordinary script variable: the script runs again whenever the screen opens, so the count starts over.
Language switching
The project has two languages: English (language 0) and Traditional Chinese (language 1). The Language Property of the General system object is bound to the Variable UI_Language, whose initial value 0 starts the project in English. The buttons on the Home screen set UI_Language to 0 or 1. In Traditional Chinese, the code excerpts on the lesson screens show translated comments; the scripts in the Custom Models are in English.