How to actually define the action inside a widget

AI 生成的摘要

In a discussion regarding how to define actions within Discourse widgets, stevenpslade asks where to implement an action for a scrollTopAnswer function attached to an anchor tag in a topic-map widget. joebuhlig suggests checking the voting plugin code for a reference. However, eviltrout clarifies that actions cannot be directly added to <a> elements in this manner. Instead, they recommend creating a dedicated Discourse widget using createWidget with a click() handler to manage the scrolling logic. Additionally, while this specific case uses jQuery directly, eviltrout notes that best practice generally involves handling actions via controllers or routes using sendWidgetAction.

Continuing the discussion from A tour of how the Widget (Virtual DOM) code in Discourse works:

In the topic-map widget I have the below code adding an action and class name to some text in the topic-map:

contents.push(h('li.secondary', [
      h('a', { className: 'top-answer-link',
              action: 'scrollTopAnswer'}, 'Jump to Top Answer')
    ]));

The class gets added fine and I see now errors. My issue however, is that I do not know where to define the action. Usually, I would define such an action in a component. As this is a widget, where should I place this code:

actions: {
    scrollTopAnswer: function() {
      alert('scrollTopAnswer');
      $('html,body').animate({scrollTop: ($('.most_liked_post').offset().top-140)}, 'slow');
    }
  }

I use a few actions in the voting plugin. You might find a good reference here:

https://github.com/joebuhlig/discourse-feature-voting/blob/master/assets/javascripts/initializers/feature-voting.js.es6#L7

You can’t just add an action to an <a> element like that. You should create a new discourse widget for that:

createWidget('jump-answer', {
  tagName: 'a.top-answer-link',
  click() {
    $('html,body').animate({scrollTop: ($('.most_liked_post').offset().top-140)}, 'slow');
  }
});

Then your contents would look like this instead:

contents.push(h('li.secondary', this.attach('jump-answer')));

I should also mention that generally I prefer actions to be handled in controllers, but this is a bit of a weird case since you’re scrolling the page with jQuery. Normally you’d have the click() function call sendWidgetAction and have a method in the Controller or Route as @joebuhlig suggested.