sendWidgetAction and Event not binding `this` to widget

This is mainly for @eviltrout but would have implications to all plugin devs.

At the moment sendWidgetAction and Event is not binding this to the widget.

sendWidgetAction(name, param) {
    return this.rerenderResult(() => {
      const widget = this._findAncestorWithProperty(name);
      if (widget) {
        return widget[name](param);
      }

      return this._sendComponentAction(name, param || this.findAncestorModel());
    });
  }

In particular we have

      if (widget) {
        return widget[name](param);
      }

Instead this could be:

     if (widget) {
        return widget[name].call(widget, param);
      }

Is there and reason we should not make this change?

4 Likes

This seems totally sensible to me. You’d expect a widget’s method to have this pointing to itself. I doubt there are any plugins that counted on this pointing at something else, and if so that’s confusing and they should be updated.

I’ve made the change:

https://github.com/discourse/discourse/commit/66f68e8faf3fa466a431ebbff7af5c1c93b1c664

5 Likes