logologo
Get Started
Tutorials
Guide
Development
Plugins
API
Home
English
简体中文
日本語
한국어
Español
Português
Deutsch
Français
Русский
Get Started
Tutorials
Guide
Development
Plugins
API
Home
logologo
RunJS Overview
Importing Modules
Rendering in Container

Global Variables

window
document
navigator

ctx

ctx.blockModel
ctx.collection
ctx.collectionField
ctx.dataSource
ctx.dataSourceManager
ctx.element
ctx.exit()
ctx.exitAll()
ctx.filterManager
ctx.form
ctx.getModel()
ctx.getValue()
ctx.getVar()
ctx.i18n
ctx.importAsync()
ctx.initResource()
ctx.libs
ctx.location
ctx.logger
ctx.makeResource()
ctx.message
ctx.modal
ctx.model
ctx.notification
ctx.off()
ctx.on()
ctx.openView()
ctx.render()
ctx.request()
ctx.requireAsync()
ctx.resource
ctx.route
ctx.router
ctx.setValue()
ctx.sql
ctx.t()
ctx.view
Previous Pagectx.notification
Next Pagectx.on()

#ctx.off()

Removes event listeners registered via ctx.on(eventName, handler). It is often used in conjunction with ctx.on to unsubscribe at the appropriate time, preventing memory leaks or duplicate triggers.

#Use Cases

ScenarioDescription
React useEffect CleanupCalled within the useEffect cleanup function to remove listeners when the component unmounts.
JSField / JSEditableFieldUnsubscribe from js-field:value-change during two-way data binding for fields.
Resource RelatedUnsubscribe from listeners like refresh or saved registered via ctx.resource.on.

#Type Definition

off(eventName: string, handler: (event?: any) => void): void;

#Examples

#Paired usage in React useEffect

React.useEffect(() => {
  const handler = (ev) => setValue(ev?.detail ?? '');
  ctx.on('js-field:value-change', handler);
  return () => ctx.off('js-field:value-change', handler);
}, []);

#Unsubscribing from resource events

const handler = () => { /* ... */ };
ctx.resource?.on('refresh', handler);
// At the appropriate time
ctx.resource?.off('refresh', handler);

#Notes

  1. Consistent handler reference: The handler passed to ctx.off must be the same reference as the one used in ctx.on; otherwise, it cannot be correctly removed.
  2. Timely cleanup: Call ctx.off before the component unmounts or the context is destroyed to avoid memory leaks.

#Related Documents

  • ctx.on - Subscribe to events
  • ctx.resource - Resource instance and its on/off methods