Instance Methods

Control a created widget instance after TradingWidget.create().

View as Markdown

Method Summary

MethodPurpose
destroy()Remove the iframe and release event listeners.
getState()Read the current SDK lifecycle state.
whenReady()Wait until the iframe finishes initialization.
setSymbol(symbol)Switch the trading pair inside the iframe.
setTheme(theme)Update the iframe theme parameter.
setLocale(locale)Switch the iframe language.
setSize(width, height)Update iframe dimensions.
reload()Reload the current iframe.
refreshAuth(reason)Trigger the widget auth refresh flow.
setTokenProvider(provider)Replace the provider used by partner-token mode.
on(event, handler)Subscribe to runtime events and receive an unsubscribe function.

destroy()

Remove the iframe, clear listeners, and emit destroyed once for the current instance.

1widget.destroy()

getState()

Read the current lifecycle state of the widget instance.

1const state = widget.getState()

Return values include loading, authenticating, ready, error, and destroyed.

whenReady()

Wait for the iframe to finish initialization before running dependent UI logic.

1await widget.whenReady()

setSymbol(symbol)

Switch the trading pair inside the iframe. If the iframe is still initializing, the SDK queues the command and sends it after ready.

1widget.setSymbol('ETHUSDT')

setTheme(theme)

Update the theme parameter used by the embedded trading iframe.

1widget.setTheme('dark')
2widget.setTheme('light')

setLocale(locale)

Switch the iframe language. Supported values are en, en-US, and zh-CN.

1widget.setLocale('en')
2widget.setLocale('en-US')
3widget.setLocale('zh-CN')

setSize(width, height)

Update the iframe DOM size and notify the iframe about the new outer size. Numeric values are converted to px.

1widget.setSize('100%', 720)

reload()

Reload the current iframe and wait for it to initialize again.

1widget.reload()

refreshAuth(reason)

Ask the iframe to refresh the current authentication flow. The optional reason value is forwarded for logging and flow control.

1widget.refreshAuth('manual')

setTokenProvider(provider)

Replace the tokenProvider used by partner-token mode. The SDK calls the provider when the iframe requests a fresh embed token.

1widget.setTokenProvider(async ({ channelId, symbol, reason }) => {
2 const resp = await fetch('/api/trading/embed-token', {
3 method: 'POST',
4 credentials: 'include',
5 headers: { 'Content-Type': 'application/json' },
6 body: JSON.stringify({ channelId, symbol, reason }),
7 })
8
9 return resp.json()
10})

on(event, handler)

Subscribe to a runtime event. The returned function removes the listener.

1const off = widget.on('order_created', (event) => {
2 console.log(event)
3})
4
5off()