Pass prevWidget to widget.updateDOM #69

Closed
nanokind wants to merge 1 commit from feature/pass-prev-widget-in-update-dom into main
nanokind commented 2025-02-22 09:09:50 +01:00 (Migrated from github.com)

In some scenarios, I would like to remove the previous event listener in widget.updateDOM, but missing the reference to previous listener in widget.updateDOM.

For exaxmple:

interface WidgetOptions {
  text: string;
  onClick: () => void;
}

class MyWidget extends WidgetType {
  constructor( private options: WidgetOptions ) {
    super()
  }

  toDOM() {
    const dom = document.createElement('span')

    dom.textContent = this.options.text
    dom.addEventListener('click', this.handleClick, false)

    return dom
  }

  handleClick() {
    this.options.onClick()
  }

  updateDOM(dom) {
    dom.textContent = this.options.text

    // Cannot remove previous listener, as we missing the reference to prevWidget.handleClick here
    dom.removeEventListener('click', ???, false)

    dom.addEventListener('click', this.handleClick, false)
    return true
  }

  ...
}

This PR pass prevWidget to widget.updateDOM to solve this.

In some scenarios, I would like to remove the previous event listener in `widget.updateDOM`, but missing the reference to previous listener in `widget.updateDOM`. For exaxmple: ```ts interface WidgetOptions { text: string; onClick: () => void; } class MyWidget extends WidgetType { constructor( private options: WidgetOptions ) { super() } toDOM() { const dom = document.createElement('span') dom.textContent = this.options.text dom.addEventListener('click', this.handleClick, false) return dom } handleClick() { this.options.onClick() } updateDOM(dom) { dom.textContent = this.options.text // Cannot remove previous listener, as we missing the reference to prevWidget.handleClick here dom.removeEventListener('click', ???, false) dom.addEventListener('click', this.handleClick, false) return true } ... } ``` This PR pass `prevWidget` to `widget.updateDOM` to solve this.
marijnh commented 2025-02-22 19:38:43 +01:00 (Migrated from github.com)

This seems easy enough to sidestep by using an onclick property instead of a listener added with addEventListener. I don't really think it is a compelling enough case to complicate the interface.

This seems easy enough to sidestep by using an `onclick` property instead of a listener added with `addEventListener`. I don't really think it is a compelling enough case to complicate the interface.
nanokind commented 2025-02-23 04:06:33 +01:00 (Migrated from github.com)

Using onclick does help solve my problem. And I cannot come up with some more convincing scenario at present. I will close this PR for now. Thanks for reviewing this marijnh.

Using onclick does help solve my problem. And I cannot come up with some more convincing scenario at present. I will close this PR for now. Thanks for reviewing this marijnh.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
codemirror/view!69
No description provided.