ctx.on()
Subscribe to context events (such as field value changes, property changes, resource refreshes, etc.) in RunJS. Events are mapped to custom DOM events on ctx.element or internal event bus events of ctx.resource based on their type.
Use Cases
Type Definition
Common Events
Event mapping rules: Events prefixed with
resource:go throughctx.resource.on, while others typically go through DOM events onctx.element(if it exists).
Examples
Field Two-way Binding (React useEffect + Cleanup)
Native DOM Listening (Alternative when ctx.on is unavailable)
Updating UI After Resource Refresh
Coordination with ctx.off
- Listeners registered using
ctx.onshould be removed at the appropriate time via ctx.off to avoid memory leaks or duplicate triggers. - In React,
ctx.offis typically called within the cleanup function ofuseEffect. ctx.offmay not exist; it is recommended to use optional chaining:ctx.off?.('eventName', handler).
Notes
- Paired Cancellation: Every
ctx.on(eventName, handler)should have a correspondingctx.off(eventName, handler), and thehandlerreference passed must be identical. - Lifecycle: Remove listeners before the component unmounts or the context is destroyed to prevent memory leaks.
- Event Availability: Different context types support different events. Please refer to the specific component documentation for details.
Related Documentation
- ctx.off - Remove event listeners
- ctx.element - Rendering container and DOM events
- ctx.resource - Resource instance and its
on/offmethods - ctx.setValue - Set field value (triggers
js-field:value-change)

